472,793 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,793 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 5691
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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.