Sign in to follow this  
suliman

uses undefined class? But pointer is ok?

Recommended Posts

Hi

I get this when i usea town as a member of another class

[CODE]error C2079: 'gameMap::sdfsdf' uses undefined class 'town'[/CODE]

Although the town.h file is included in the file so it should know what that class is. Its not "undefined".
What confuses me more is pointers are ok in that file, not just include a class itself:

[CODE]
std::list<town *> townList; //ok
town * anotherTest; //ok
town testTown; //NOT ok
[/CODE]

Whats all this about?
Thanks
Erik

Share this post


Link to post
Share on other sites
I'm not sure why it's still considered undefined when you include town.h because I don't know what's in that file.

But a pointer is okay because pointers are always the same size, so at compile time the compiler knows the size of the gameMap class but not if you include an instance of the town class directly because it's undefined thus unknown size.

Hope this helps!

Share this post


Link to post
Share on other sites
You can use a "forward declaration" in C++ that only tells the compiler that there is a class or structure with a certain name, but nothing more:

[CODE]
class town; // forward declaration
town *plainPointer;
std::unique_ptr<town> wrappedPointer;
[/CODE]

This pointer can be passed on, but since the compiler doesn't know anything about it, you cannot call methods on it or safely delete it.

Your error message sounds exactly like that is your problem, but you haven't told us enough to say why this is happening in your case. If I should make a guess, perhaps you are including town.h from another header and accidentally set up a circular header dependency, which then causes this due to the include guards being triggered in a certain way:

[CODE]
#ifndef OTHER_H
#define OTHER_H

#include "town.h"

class town;

class other {
town the_town;
};

#endif // OTHER_H
[/CODE]
[CODE]
#ifndef TOWN_H
#define TOWN_H

#include "other.h"

class town {
other *owner;
};

#endif // TOWN_H
[/CODE]

In this case, when town.h is compiled, TOWN_H gets defined. Then other.h gets included. When other.h tries to include town.h back, TOWN_H is already defined, thus other.h gets processed without the contents of town.h, leaving only the forward declaration for the compiler.

[b]EDIT[/b]: Is the code snippet feature currently broken? [b]EDIT2[/b]: Tried again, works. Edited by Cygon

Share this post


Link to post
Share on other sites
There is a common use case for this. In header files, you maybe use a pointer to a class, but the header file contains nothing that depends on the actual content of this class. In that case, it may be better to use a forward declaration of the class (Cygon showed). That way, you don't need the header file to also include another header file that declares the exact content of the class. And thus you can also avoid circular dependencies.

[quote name='Cygon' timestamp='1343377524' post='4963564']
EDIT: Is the code snippet feature currently broken? Tried with [ code ] and [ source ].
[/quote]

I think it depends on an external service. Just go ahead and use it, it will show nice later on. Edited by larspensjo

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this