Skip to main content
GameDev.net gamedev.net
🔒 Locked

"recursive" class reference (Solved)

Started by The C modest god Jul 14, 2005 at 5:01 AM 12 replies 1.5k views
Original Post
The C modest god
The C modest god
Consider the following code:

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]
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Helter Skelter
Helter Skelter
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);}

nonpop
nonpop
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
The C modest god
The C modest god
I have a problem with recursive headers
#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'
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
The C modest god
The C modest god
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Nitage
Nitage
You need to include the header which LinkedList is defined in.

Telastyn
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.

The C modest god
The C modest god
I did.
Its datalists.h
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
The C modest god
The C modest god
I tried to do the following:

// 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?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
The C modest god
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.

It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Telastyn
Telastyn
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. I'm not sure of the correct syntax for something like that.
The C modest god
The C modest god
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.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.