Singletons (class templates and pure virtual functions)

Started by
22 comments, last by L. Spiro 12 years, 6 months ago
hi

Im trying to create a singleton but visual studio is rejecting my private constructor.
Why is this? aren't singleton constructors meant to be private or is it that they are shunned by visual studio?

class CStart_Menu :
public CStates<CGAME_MANAGER>
{
private:

CStart_Menu();

public:
//BEGIN STATE:
virtual FAST_BOOL Enter(CGAME_MANAGER* qp_laststate)


[output]1>Start_Menu.obj : error LNK2019: unresolved external symbol "private: __thiscall
CStart_Menu::CStart_Menu(void)" (??0CStart_Menu@@AAE@XZ) referenced in function
"public: static class CStart_Menu * __cdecl CStart_Menu::Instance(void)" (?
Instance@CStart_Menu@@SAPAV1@XZ)

1>Start_Menu.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall
CStates<class CGAME_MANAGER>::~CStates<class CGAME_MANAGER>(void)" (??1?
$CStates@VCGAME_MANAGER@@@@UAE@XZ) referenced in function "public: virtual __thiscall
CStart_Menu::~CStart_Menu(void)" (??1CStart_Menu@@UAE@XZ)

1>c:\users\portsmouthuni\documents\visual studio
2010\Projects\consolegamestate\Debug\consolegamestate.exe : fatal error LNK1120: 2
unresolved externals

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
[/output]
Advertisement
It's complaining that it can't find the function bodies for the functions. You might want to implement them.

It's complaining that it can't find the function bodies for the functions. You might want to implement them.


I've done that yet its till there. its been 5 hours now and i'm seriously behind
The compiler disagrees. Show us how you've implemented them.
this is the instance I want my singletons to be controlled by


#pragma once
#ifndef _GAME_MANAGER_ZUMA_H
#define _GAME_MANAGER_ZUMA_H
#include "StateMachine.h"
//*********************************************************************************************
//Purpose: Managing the game state
//Type:
//
//*********************************************************************************************
class CGAME_MANAGER{
private:
//use our state machine
CStateMachine<CGAME_MANAGER> * qmpStateMachine;

public:
CGAME_MANAGER();


~CGAME_MANAGER(void);

//add class methods here...
void Update(void);

CStateMachine<CGAME_MANAGER>* Use_StateMachine();

};

#endif



Basically here is where I create my state;

#pragma once
#ifndef _MY_STATES_MENU_H
#define _MY_STATES_MENU_H
#include "states.h"
#include "GAME_MANAGER.h"
/****************************************************
//Purpose: This is the menu at the start of the game
*****************************************************/
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(CGAME_MANAGER* qp_laststate);


//EXECUTE OUR STATE:
//Now lets get things rolling
virtual void Execute(CGAME_MANAGER* q);

//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<CGAME_MANAGER>* Exit(CGAME_MANAGER* q);
//SINGLETON DECLARATION
CStart_Menu* Instance();


//virtual ~CStart_Menu(void);
};

#define stSTART_MENU CStart_Menu::Instance()

#endif


And finally here is where it rejects my singleton :(sad.gifsad.gif:(

#include "GAME_MANAGER.h"
#include "Start_Menu.h"

CGAME_MANAGER qGameManager;

int main()
{
qGameManager.Use_StateMachine()/*->ChangingState(stSTART_MENU)*/;

qGameManager.~CGAME_MANAGER();
return 0;
}

There's a distinct lack of function definitions in the code you just posted. The function definitions are the parts with the curly braces that have statements inside that determine what the functions actually do. Your main() function has a definition, but nothing else seems to.
Do you know the difference between a declaration and a definition? If not, now would be the time to learn it via Google.

As a side note, any time you need to use singletons, you have done something wrong. There is always a better choice.
Even Morgan Stanley and Square Enix agree.


L. Spiro


[EDIT]
Would the person who provided me with this lovely red -1 explain whether it was due to my suggestion regarding Google or because of a personal opinion that there is some justification to use a singleton?

If you disagree with something, you should explain why.
[/EDIT]

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

I've got that stuff defined in the .cpp files, but thought i'd omit it to stop this seeming like code dumping.

This has now given me a real headache
Prove it. Show us the .CPP files.

Because right now there are only 2 possibilities.
#1: Either you didn’t write the functions.
#2: Or you are using static libraries and are not linking to them from the main project.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Do you have function bodies for CStart_Menu::CStart_Menu() and CStates::~CStates() ?

Note that you should not be explicitly calling a destructor, unless you are 200% sure of what you are doing. The compiler will emit a call to the destructor after main(), so you've actually asked to run the destructor twice there.

This topic is closed to new replies.

Advertisement