Jump to content
  • Advertisement
Sign in to follow this  
The C modest god

"recursive" class reference (Solved)

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.h
class B

class A {
public:
void do(B &);
};

// file: B.h
class 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);
}

Share this post


Link to post
Share on other sites
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 first
class 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

Share this post


Link to post
Share on other sites
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<class ErrorMessage>'

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!