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]
🔒 Locked
"recursive" class reference (Solved)
Advertisement
Original Post
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>'
Does this article answer your question?
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.
I did.
Its datalists.h
Its datalists.h
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?
Quote:
Original post by Telastyn 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.
These are two generic class that needs to use each other. One is for handling errors, the other is a basic data type.
Logically they should be seperated, eventhough they need to use each other.
Besides, if something can be done in one file, but it cant be done in two seperate files. That means that the support for writting programs in seperate files suck big time.
Quote:
Original post by The C modest god Quote:
Original post by Telastyn 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.
These are two generic class that needs to use each other. One is for handling errors, the other is a basic data type.
Logically they should be seperated, eventhough they need to use each other.
Besides, if something can be done in one file, but it cant be done in two seperate files. That means that the support for writting programs in seperate files suck big time.
Indeed, logically they should be seperated. But the dependancies/design you describe means they're completely inseperable.
Anyways, your problem is perhaps that you're trying to pre-declare LinkedList_ErrorMessage as a class, when it's not a class, it's a template <class T> class. I'm not sure of the correct syntax for something like that.
I have solved the problem.
The header file with the stream list was a template only. And it only used a class from the Error file in its code implementation of the methods. Not in the declerations of the classes in the stream list.
So I moved the include of the error after all the classes of the stream list but before the implementation of its methods. It worked.
The header file with the stream list was a template only. And it only used a class from the Error file in its code implementation of the methods. Not in the declerations of the classes in the stream list.
So I moved the include of the error after all the classes of the stream list but before the implementation of its methods. It worked.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement