class B
class A {
public:
void do(B &);
};
class B {
public:
void do(A &);
};
This code will compile.
However, why it is impossible to put these two classes in two different .h files?
[Edited by - The C modest god on July 14, 2005 12:56:55 PM]
"recursive" class reference (Solved)
Consider the following code:
Quote:Original post by The C modest god
This code will compile. However, why it is impossible to put these two classes in two different .h files?
It's not:
// file: A.hclass Bclass A { public: void do(B &);};// file: B.hclass A;class B { public: void do(A &);};
However before either class can access an object of the other type the full class declaration must be visible.
// file: B.cpp#include "A.h"#include "B.h"void B::do(A &a){ a.do(*this);}// File A.cpp#include "A.h"#include "B.h"void A::do(B &b){ printf("%p\n", &b);}
Quote:Original post by The C modest god
This code will compile.
Actually it won't, because you're using a reserved word (do) as a function name and you forgot a semicolon ;)
Quote:However, why it is impossible to put these two classes in two different .h files?
It isn't. I tried and it worked:
// classa.h:class B;class A {public: void dostuff (B& b) {}};// classb.h:class A; // You didn't have this, and it isn't // required, if classa.h is included firstclass B{public: void dostuff (A& a) {}};// main.cc:#include "classa.h"#include "classb.h"int main (){ A a; B b; a.dostuff (b); b.dostuff (a);}
EDIT: Too late... :P
I have a problem with recursive headers
I get the following error:
error C2079: 'List' uses undefined class 'LinkedList<class ErrorMessage>'
#ifndef ERRORHANDLE#define ERRORHANDLE#include "DataLists.h"using namespace std;class ErrorMessage { public: char * Message; HRESULT Code; ErrorMessage (); ErrorMessage (const ErrorMessage & ErrMsg); ~ErrorMessage ();};template <class T> class LinkedList;class ErrorList { public: char * GetError (HRESULT Code); void InsertError (HRESULT Code, char * Message); private: LinkedList<ErrorMessage> List;};
I get the following error:
error C2079: 'List' uses undefined class 'LinkedList<class ErrorMessage>'
No it really doesnt help.
My problem is more severe.
As you can see in the code I posted I included the relevant header file and declared about the class I am using.
It doesnt help.
Is there a solution for such a problem?
Edit: When I turn list into a pointer to that class it works, but I dont want a pointer.
My problem is more severe.
As you can see in the code I posted I included the relevant header file and declared about the class I am using.
It doesnt help.
Is there a solution for such a problem?
Edit: When I turn list into a pointer to that class it works, but I dont want a pointer.
Quote:Original post by The C modest god
Is there a solution for such a problem?
Yeah, put them in the same header. Seriously, if the two classes are that intertwined, you can never include one without the other anyways.
I tried to do the following:
On "class LinkedList_ErrorMessage; " I get the following error:
error C2371: 'LinkedList_ErrorMessage' : redefinition; different basic types
Why am I getting such an error?
Why it thinks I redfine the class? is that a bug of the compiler?
// ErrorHandle.h#ifndef ERRORHANDLE#define ERRORHANDLE#include "DataLists.h"using namespace std;class ErrorMessage { public: char * Message; HRESULT Code; ErrorMessage (); ErrorMessage (const ErrorMessage & ErrMsg); ~ErrorMessage ();};//template <class T> class LinkedList;class LinkedList_ErrorMessage; // <= Error!class ErrorList { public: char * GetError (HRESULT Code); void InsertError (HRESULT Code, char * Message); private: LinkedList_ErrorMessage List;}// DataLists.h#ifndef DATALISTS#define DATALISTS#include "ErrorHandle.h"template <class T>struct Linked { Linked<T> * pNext; T Data;};template <class T> class LinkedListPointer;template <class T>class LinkedList { public: LinkedListPointer<T> CreateLinkedListPointer(); BOOL IsEmpty(); T & Peek(); void Pop(); void Push (const T & Data); LinkedList(); ~LinkedList(); private: void LocalFree(); Linked<T> * pList;};class ErrorMessage;typedef class LinkedList<ErrorMessage> LinkedList_ErrorMessage;
On "class LinkedList_ErrorMessage; " I get the following error:
error C2371: 'LinkedList_ErrorMessage' : redefinition; different basic types
Why am I getting such an error?
Why it thinks I redfine the class? is that a bug of the compiler?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement