Using 2 Classes that include each other

Started by
14 comments, last by owl 19 years, 3 months ago
Basically here's what I have:

//Class A
#include "ClassB"

class A{
A(B)
}

//Class B
#include "ClassA"
B()...
private:
A;
Is there any way to make this work?
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
Advertisement
you need a class declaration.

//Class A#include "ClassB"class A{A(B)}//Class Bclass ClassA;B()...private:A;


note the "class ClassA". this tells the compiler "there is a class called ClassA out there, and you will know about him at compile time".
FTA, my 2D futuristic action MMORPG
Then it tells me I'm using an undefined class.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
because when you do forward declaration you can only create the object dinamically.

I mean:
class a;class b{   a myA; // <---wont work};


You need to make myA a pointer:
class a;class b{   a* myA; // <---WILL work   b() //constructor   {     a = new a();   }};
[size="2"]I like the Walrus best.
Quote:Original post by Sfpiano
Then it tells me I'm using an undefined class.


Because a class declaration only allows you to create pointers, references to that class, or to declare functions that return or take it as parameter.

Code that actually creates variable of the class type require a full definition, as there is no other way for the compiler to know the actual size of the variable.

In your example, since A only uses B as a function parameter, it can make do with a declaration for B. However, B has a member of type A (not A* nor A&) and thus needs the full definition for A.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Ok, when I do that it tells me my class has no constructors:
A::Init() {
a = new B();
}
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
then add a constructor B::B(){} to your class.

and make it public! I wonder why it doesn't use the default one.

ah, don't forget to do #include "B.h" in A.cpp
[size="2"]I like the Walrus best.
I already did.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."
read above ^
[size="2"]I like the Walrus best.
Ok, here's my code:
//Graph.h#include "Geo.h"class Graph {public: 	Graph();	Graph( Geo &obj );...}//Geo.hclass Graph;class Geo {private: 	Graph* graph;...}//Geo.cppGeo::Geo() {graph = new Graph();}


And it tells me Graph has no constructors.
//------------------------------------------------------------------------------------------------------The great logician Bertrand Russell once claimed that he could prove anything if given that 1+1=1. So one day, some fool asked him, "Ok. Prove that you're the Pope." He thought for a while and proclaimed, "I am one. The Pope is one. Therefore, the Pope and I are one."

This topic is closed to new replies.

Advertisement