473,412 Members | 2,027 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Problem : cross reference between classes

I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
Dec 31 '07 #1
3 5745
xd*****@gmail.com wrote:
I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
This smells like some design problem. What object from where
is to be accessed?
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
I'ts straightforward to solve, jus put the
implementation out of the class declaration:

----- 8< --- [class_ab.h] (declaration) ------

class A {
void f1(void);
public:
void f2(void);
};

class B {
void g1(void);
public:
void g2(void);
};

----- 8< --- [class_ab.cxx] (implementaion)---

#include "classab.h"
#include <cstdio>

void A::f1(void) {
B b;
b.g2();
printf("we are in A::f1\n");
}

void A::f2(void) {
printf("we are in A::f2\n");
}

void B::g1(void) {
A a;
a.f2();
printf("we are in B::g1\n");
}

void B::g2(void) {
printf("we are in B::g2\n");
}

----- 8< ----------------------------------
$gcc -o classab class_ab.cxx -lstdc++
Regards

Mirco
Dec 31 '07 #2
"xd*****@gmail.com" <xd*****@gmail.comwrote:
I have two classes , say A and B. A contains methods named f1() ,
f2() and B contains methods named g1(), g2().
f1() will invoke g2() and g1() will invoke f2(). This situation is
kinda like two things interact with each other.
But the program couldn't pass the compliation. g++ says " invalid use
of undefined type of ......" .
As far as I know, forward declaration couldn't solve this problem, and
I have tried. Both of the definition of the two classes has to come
first which is not possible.
This can be solved by redesign A and B, but I wish I can find some
other solutions here, any advice will be welcome .
http://www.parashift.com/c++-faq-lit....html#faq-39.1
1
Dec 31 '07 #3
Many thanks !
I found the cure, I was wrong about that forward declaration couldn't
solve this problem.
Here is my solution:
---------class_a.h----------
class B;
class A {
......
};
---------class_a.cpp----------
#include "class_a.h"
#include "class_b.h"
void A::f1() {
.........// invoke method in B
}
---------class_b.h------------
class A;
class B {
.....
};
---------class_b.cpp----------
#include "class_a.h"
#include "class_b.h"
void B::g1() {
.........// invoke method in A
}
Jan 1 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
2
by: Sidorf | last post by:
Hi It's a little hard for me to explain my problem, but i'll try. I have an application in which i have a ManagerClass, many ClientClass-es ans some ControlClass. The ManagerClass creates one...
3
by: Solution Seeker | last post by:
Hi All, I am here with a Query and need a Solution for it. The Query is as Follows, We have 3 Projects in a Solution - Say UI, CMN and PRD First One Deals with UI Forms Second One Deals...
6
by: Picho | last post by:
Hi all. I have a webservice and a windows app. both of them reference the same class library called WebServiceTest.Core that defines a class called Class1. the webservice exposes a method...
3
by: musosdev | last post by:
Hi guys I've got the following error on a project which is running locally on a vs2005 machine (built in webserver), trying to connect to my win2k3 server active directory. the error is... ...
12
by: Joel Byrd | last post by:
I am making an AJAX call when any one of a number of inputs in a form (search criteria) are clicked, checked, changed, etc. When the user clicks, checks, whatever, I am trying to display a...
34
by: Simon Wigzell | last post by:
document...focus() will scroll the form to move the specified text field into view on everything I have tried it with except Safari on the MAC. The form doesn't move. Any work around? Thanks.
7
by: for.fun | last post by:
Hi everybody, I have the following problem : B class need A::MyEnum type and A class need B::MyEnum type. In both case, the class type is incomplete so it is obvious that the ::MyEnum can not...
9
by: AceKnocks | last post by:
I am working on a framework design problem in which I have to design a C++ based framework capable of solving three puzzles for now but actually it should work with a general puzzle of any kind and I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.