Skip to main content
GameDev.net gamedev.net
🔒 Locked

Problems with OOP

Started by colinisinhere May 13, 2004 at 5:19 PM 2 replies 300+ views
Original Post
colinisinhere
colinisinhere
I''ve read many posts with a lot of good information on OOP, but I''m having troubles with my own program, here is a basic outline of what my how my program is written:
///////////////////////////////////////

//CGame.h

///////////////////////////////////////


#ifndef __CGAME_H_INCLUDED__
#define __CGAME_H_INCLUDED__

include "Client.h"
class Client;

class CGame
{
public:
   Client player;
   //Other crap

}
#endif

///////////////////////////////////////

//Client.h

///////////////////////////////////////


#ifndef __CLIENT_H_INCLUDED__
#define __CLIENT_H_INCLUDED__

include "CGame.h"
class CGame;

class Client
{
public:
   CGame world;
   //Other crap

}
#endif

///////////////////////////////////////

//main.cpp

///////////////////////////////////////


#include "CGame.h"

INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
{	
   CGame game;
   //Uses CGame functions

   return 0;
}
But I get the following error: CGame.h(55) : error C2079: ''player'' uses undefined class ''Client'' I have tried many ways around this but with no luck! What have I done wrong?
fyhuang
fyhuang
You don''t need to prototype the class:

class Client

Before the CGame class. Or you can prototype it BEFORE the include statement. Same thing in the Client.h file.
- fyhuang [ site ]
Zahlman
Zahlman
You cannot directly embed class A in class B and also directly embed class B in class A. The embedded class would need to embed its own class, etc., leading to an infinite-size data structure.

To get around this problem, you need to use a pointer instead in at least one of the classes. If there is a logical pairing of objects, you may prefer to use it in both places (unless perhaps there is a clear sense that one class "owns" the other while the other just "knows about" its owner).

The compiler prevents you from creating an infinite sized data structure at compile time, by requiring a full class definition (not just a forward declaration) to have been seen ahead of time when you use an object (not a pointer-to-object) as a member variable.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.