virtual class & template error

hi

how is a class pure virtual class with a template inherited?


Pure virtual class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
template<class xUsers_states>
class CStates
{

	CStates(void);
	
public:
		
	//BEGIN STATE:
	//take message from previus state 
	//to see if transisation is possible
	// return 1 for success, 0 for fail
    virtual FAST_BOOL Enter(xUsers_states*)=0;
	
	//EXECUTE OUR STATE:
	//Now lets get things rolling
	virtual void Execute(xUsers_states*)=0;
	
	//EXITING STATE:
	//Pass message to our successor
	//return our address to let the successor know, 
	//this message affects the call of action of our successor
	virtual CStates<xUsers_states>* Exit(xUsers_states*)=0;
	
	//virtual destructors are needed for pure virtual classes
	//its children will handle differently
	virtual ~CStates(void);
};


Here: my attempt to inherite it, the issue here is that it refuses to achknowledge my "CGAME_MANAGER" even though I've inherited it in the template through "public CStates<CGAME_MANAGER>".

Whats my mistake?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class CStart_Menu :
	public CStates<CGAME_MANAGER>
{
private:
	CStart_Menu(void);

public:
	//BEGIN STATE:
	//take message from previus state 
	//to see if transisation is possible
	// return 1 for success, 0 for fail
    virtual FAST_BOOL Enter(CStates*);
	
	//EXECUTE OUR STATE:
	//Now lets get things rolling
	virtual void Execute(CGAME_MANAGER*);
	
	//EXITING STATE:
	//Pass message to our successor
	//return our address to let the successor know, 
	//this message affects the call of action of our successor
	virtual CStates* Exit(CStates*);

	//SINGLETON DECLARATION
	static CStart_Menu * Instance();
	
	virtual ~CStart_Menu(void);
};


"CGAME_MANAGER" does not define a type, so you need a template declaration for it above your definition of "CStart_Menu". I'm glad to see a user in the beginner forums using objects, good work so far.
I think you have the same problem as here:
http://www.cplusplus.com/forum/general/51699/

Basically you need to explicitly tell the compiler a member function of base class exists. See the other post for possible options
In the future, it would really help if you posted the actual errors rather than your interpretation of what the error means.

You have 2 problems here:

1) Your Enter and Exit functions take a xUsers_states* as a parameter in the parent class, but a CState* in the child class (note this is wrong for 2 reasons: one is that they don't match, and two is that CState isn't a class -- it's a template -- so you can't have a pointer to it.)

One solution here would be to change CStart_Menu so that Enter and Exit both take CGAME_MANAGER* as a parameter.



2) CState's ctor is private, so CStart_Menu won't be able to access it (so you won't be able to derive from it). One solution is to make it protected instead of private.
CStart_Menu was going to be a singleton thats why its private
Protected is the same as Private except that derived classes can inherit it.
Here are the errors:

But I now think I'm at the age where posting just errors may seem like code dumping, so I been trying to explain them instead. but here are the errors anyway
1>------ Build started: Project: Game state machine, Configuration: Debug Win32 ------
1>Build started 10/10/2011 14:59:59.
1>InitializeBuildStatus:
1>  Touching "Debug\Game state machine.unsuccessfulbuild".
1>ClCompile:
1>  game_main.cpp
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(11): error C2065: 'CGAME_MANAGER' : undeclared identifier
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(12): error C2955: 'CStates' : use of class template requires template argument list
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\states.h(22) : see declaration of 'CStates'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(21): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(25): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(31): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(37): warning C4511: 'CStart_Menu' : copy constructor could not be generated
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(10) : see declaration of 'CStart_Menu'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(37): warning C4512: 'CStart_Menu' : assignment operator could not be generated
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(10) : see declaration of 'CStart_Menu'
1>  GAME_MANAGER.cpp
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(11): error C2065: 'CGAME_MANAGER' : undeclared identifier
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(12): error C2955: 'CStates' : use of class template requires template argument list
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\states.h(22) : see declaration of 'CStates'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(21): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(25): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(31): error C2061: syntax error : identifier 'CGAME_MANAGER'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(37): warning C4511: 'CStart_Menu' : copy constructor could not be generated
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(10) : see declaration of 'CStart_Menu'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(37): warning C4512: 'CStart_Menu' : assignment operator could not be generated
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\start_menu.h(10) : see declaration of 'CStart_Menu'
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\game_manager.cpp(9): error C2664: 'CStateMachine<qXType_state>::SetCurrentState' : cannot convert parameter 1 from 'CStart_Menu *' to 'CStates<xUsers_states> *'
1>          with
1>          [
1>              qXType_state=CGAME_MANAGER
1>          ]
1>          and
1>          [
1>              xUsers_states=CGAME_MANAGER
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>  Start_Menu.cpp
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\statemachine.h(46): error C2664: 'CStateMachine<qXType_state>::State_Active' : cannot convert parameter 1 from 'CStates<xUsers_states> *' to 'CStates<xUsers_states> &'
1>          with
1>          [
1>              qXType_state=CGAME_MANAGER
1>          ]
1>          and
1>          [
1>              xUsers_states=CGAME_MANAGER
1>          ]
1>          and
1>          [
1>              xUsers_states=CGAME_MANAGER
1>          ]
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\statemachine.h(44) : while compiling class template member function 'unsigned char CStateMachine<qXType_state>::ChangingState(CStates<xUsers_states> *)'
1>          with
1>          [
1>              qXType_state=CGAME_MANAGER,
1>              xUsers_states=CGAME_MANAGER
1>          ]
1>          g:\year2\game software engineering gameng\mazuma engine\game state machine\game_manager.h(41) : see reference to class template instantiation 'CStateMachine<qXType_state>' being compiled
1>          with
1>          [
1>              qXType_state=CGAME_MANAGER
1>          ]
1>g:\year2\game software engineering gameng\mazuma engine\game state machine\statemachine.h(51): error C3867: 'CStates<xUsers_states>::Exit': function call missing argument list; use '&CStates<xUsers_states>::Exit' to create a pointer to member
1>          with
1>          [
1>              xUsers_states=CGAME_MANAGER
1>          ]
1>  Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.29
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
So yeah... in addition to the above 2 problems, CGAME_MANAGER isn't defined. You need to forward declare it:

1
2
3
4
5
6
class CGAME_MANAGER;

template<class xUsers_states>
class CStates
{
  //... 


You also have errors in unposted code....

 
error C2664: 'CStateMachine<qXType_state>::State_Active' : cannot convert parameter 1 from 'CStates<xUsers_states> *' to 'CStates<xUsers_states> & 


This one seems pretty self explanitory. You're passing a pointer to a function that takes a reference.
thanks Disch, those errors where fixed, but now I've got the using class issue:

basically, it seems to accept the declaration in the headerfile, but I can't redeclare these pure I cant redeclare them in the .cpp file or even tell them that I need them to be virtual.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
extern class CGAME_MANAGER; 

class CStart_Menu :
	public CStates<CGAME_MANAGER>
{
private:
	CStart_Menu(void);

public:
	//BEGIN STATE:
	//take message from previus state 
	//to see if transisation is possible
	// return 1 for success, 0 for fail
	using CStates<CGAME_MANAGER>::Enter(CGAME_MANAGER*);


	//EXECUTE OUR STATE:
	//Now lets get things rolling
	using CStates<CGAME_MANAGER>::Execute(CGAME_MANAGER*);
	
	//EXITING STATE:
	//Pass message to our successor
	//return our address to let the successor know, 
	//this message affects the call of action of our successor
	using CStates<CGAME_MANAGER>::Exit(CGAME_MANAGER*);

	//SINGLETON DECLARATION
	static CStart_Menu * Instance();
	
	virtual ~CStart_Menu(void);
};

#define stSTART_MENU CStart_Menu::Instance() 

this section is the problem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
//eturn 1 for success, 0 for fail
       using CStates<CGAME_MANAGER>::Enter(CGAME_MANAGER* qp_laststate)
	{
		//Test if there is a transisation
		//from the previous state
		qp_laststate->Use_StateMachine()->CurrentState();
		/* 
		if ((qp_laststate->Use_StateMachine()->CurrentState())== (extern)knownstate)
		if(!(execute trasition()))//returns true if transition complete
		{return 0}//else carry on with transition
		else if (try known transition number 2)
		.....
		
		
		
		else
		//ADD ANIMATION:

		*/
		//LISTEN FOR BUTTONS (skip option here)

		
		//transition complete
		return 1;
	}
	
Last edited on
I'm not sure I understand your question.

And why did you put extern and using everywhere? By putting those usings there, it's like you deleted the function prototypes.

Get rid of that riff-raff. You don't need it.
I meant to say how could I define the following virtual function in a .cpp file
using CStates<CGAME_MANAGER>::Exit(CGAME_MANAGER*);
Topic archived. No new replies allowed.