Archived

This topic is now archived and is closed to further replies.

Really simple problem

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

this won''t compile
  
#define INIT		0
#define MAINMENU	1
#define PLAY		2
#define QUIT		3

class AppState;

class Application {
	public:
		Application( int CmdShow );
		~Application();

		Run();

	private:
		int nCmdShow;

		AppState State;
		bool Paused;

		InputManager InputMan;
		
		DirectDrawManager DDMan;
		DirectDrawSurface PrimarySurface;

		bool DoFrame();

		LRESULT CALLBACK MsgProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
};

class AppState {
	public:
		int GetState() { return now; }
		int GetLast() { return last; }
		void SetState( int set_to );
		bool (*pDo)();
	private:
		int now;
		int last;
};

void AppState::SetState( int set_to )
{
	last = now;
	now = set_to;
}
  
it gives me the following: application.h(34) : error C2079: ''State'' uses undefined class ''AppState'' application.cpp(18) : error C2228: left of ''.SetState'' must have class/struct/union type application.h(34) : error C2079: ''State'' uses undefined class ''AppState'' Any help is appreciated ~Vendayan

Share this post


Link to post
Share on other sites
Since the error is "AppState undefined", you should move up the definition of AppState to above the definition of Application. Forward declarations (your "class AppState") only work when using a reference or pointer to an undefined object. To use an object "in space", as we say, it needs to be defined. Application uses AppState in space on the line "AppState State;"

Share this post


Link to post
Share on other sites
Try This.

      
#define INIT 0
#define MAINMENU 1
#define PLAY 2
#define QUIT 3


class AppState {
public:
int GetState() { return now; }
int GetLast() { return last; }
void SetState( int set_to );
bool (*pDo)();
private:
int now;
int last;void AppState::SetState( int set_to ){ last = now; now = set_to;}
};

class Application {
public:
Application( int CmdShow );
~Application();
Run();
private:
int nCmdShow;
AppState State;
bool Paused;
InputManager InputMan; DirectDrawManager DDMan; DirectDrawSurface PrimarySurface;
bool DoFrame();
LRESULT CALLBACK MsgProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
};




,Jay

Edited by - Jason Zelos on February 20, 2002 7:56:40 PM
I will get rid of long lines.....

Edited by - Jason Zelos on February 20, 2002 7:57:50 PM

Share this post


Link to post
Share on other sites
A forward declaration is only sufficient if all you need is the class name. Pointers and references work because they're really just memory addresses, no real class info is required to use those. Creating an object on the stack doesn't work because the compiler needs more info to construct the object.

    
class A;

class B
{
A* ptr; // works!

A& ref; // works!

A obj; // doesn't work


...
};



Using methods doesn't work either because they haven't been declared yet.

        
class A;

class B
{
A* ptr;
A& ref;

...

void func()
{
ptr->method(); // doesn't work

ref.method(); // doesn't work either

}
};


btw: you should really get rid of those #defines and use an enum instead:

  
enum state
{
init,
main_menu,
play
quit
};


Edited by - kvh on February 21, 2002 6:18:53 AM

Share this post


Link to post
Share on other sites