uses undefined class? But pointer is ok?

Started by
5 comments, last by larspensjo 11 years, 9 months ago
Hi

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

error C2079: 'gameMap::sdfsdf' uses undefined class 'town'

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:


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


Whats all this about?
Thanks
Erik
Advertisement
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!
Pointers and references can be created to an undefined but declared type, but you need a defined type to create objects or access members of a class via any kind of reference.

For example, if you only forward declare the class name the type is undefined, but the compiler now knows it's a type and you can create pointers and references to that class. Try to access any members from the anotherTest pointer in your example, or just allocate an object for the pointer, and you will see that you now need the class to be defined and not just declared.
But how did i declare the class but not define it? Im confused...
I haven't seen your code so I cannot say.
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:


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


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:


#ifndef OTHER_H
#define OTHER_H

#include "town.h"

class town;

class other {
town the_town;
};

#endif // OTHER_H


#ifndef TOWN_H
#define TOWN_H

#include "other.h"

class town {
other *owner;
};

#endif // TOWN_H


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.

EDIT: Is the code snippet feature currently broken? EDIT2: Tried again, works.
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
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.


EDIT: Is the code snippet feature currently broken? Tried with [ code ] and [ source ].


I think it depends on an external service. Just go ahead and use it, it will show nice later on.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/

This topic is closed to new replies.

Advertisement