Jump to content
  • Advertisement
Sign in to follow this  
suliman

uses undefined class? But pointer is ok?

This topic is 2248 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

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

Share this post


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

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:


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


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. Edited by larspensjo

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!