Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


uses undefined class? But pointer is ok?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 suliman   Members   -  Reputation: 589

Like
0Likes
Like

Posted 27 July 2012 - 01:42 AM

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

Sponsor:

#2 hannesnisula   Members   -  Reputation: 1045

Like
0Likes
Like

Posted 27 July 2012 - 01:45 AM

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!

#3 Brother Bob   Moderators   -  Reputation: 9126

Like
5Likes
Like

Posted 27 July 2012 - 01:54 AM

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.

#4 suliman   Members   -  Reputation: 589

Like
0Likes
Like

Posted 27 July 2012 - 02:10 AM

But how did i declare the class but not define it? Im confused...

#5 Brother Bob   Moderators   -  Reputation: 9126

Like
0Likes
Like

Posted 27 July 2012 - 02:21 AM

I haven't seen your code so I cannot say.

#6 Cygon   Crossbones+   -  Reputation: 1193

Like
4Likes
Like

Posted 27 July 2012 - 02:25 AM

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, 27 July 2012 - 04:55 AM.

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.

#7 larspensjo   Members   -  Reputation: 1561

Like
0Likes
Like

Posted 27 July 2012 - 03:53 AM

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, 27 July 2012 - 03:55 AM.

Current project: Ephenation.
Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS