I come from a .NET and Java background, and I'm trying to create a simple scene manager for my game. It's an inline header file, and I'm getting errors compiling.
#pragma once
#include "Scene.h"
class SceneManager
{
private:
static Scene currentScene;
public:
SceneManager()
{
}
static void SetScene(Scene scene)
{
currentScene = scene;
}
static Scene GetScene()
{
return currentScene;
}
};
EDIT: I am getting this error:
Error 1 error LNK2001: unresolved external symbol "private: static class Scene SceneManager::currentScene" (?currentScene#SceneManager##0VScene##A) c:\Users\Justin\documents\visual studio 2013\Projects\Noeron\Noeron\main.obj Noeron
Static member variables need to be not only declared, but defined. The declaration belongs in the header file, and the definition should go into a source file - you only want one of them in the entire program.
Scene SceneManager::currentScene;
Related
Hope some one could help me with this very trivial error which I have not idea how to proceed :)
Here's the header file:
#ifndef __InputHandler__
#define __InputHandler__
#include "SDL.h"
#include <SDL_image.h>
#include <vector>
#include <iostream>
enum mouse_buttons {
LEFT = 0,
MIDDLE = 1,
RIGHT = 2,
};
class InputHandler {
public:
bool getMouseButtonState(int buttonNumber) {
return m_mouseButtonStates[buttonNumber];
}
std::vector<bool> m_mouseButtonStates;
static InputHandler* Instance() {
if (s_pInstance == 0) {
s_pInstance = new InputHandler();
}
return s_pInstance;
}
void update();
void clean();
private:
InputHandler();
~InputHandler() {}
static InputHandler* s_pInstance;
};
#endif
And I call the InputHandler::Instance() function in another class definition Player.cpp
#include "InputHandler.h"
void Player::handleInput() {
if (InputHandler::Instance()->getMouseButtonState(LEFT)) {
m_velocity.setX(1);
}
}
Here's the error that I get:
Player.obj : error LNK2001: unresolved external symbol
"private: static class InputHandler * InputHandler::s_pInstance"
I honestly don't know what's going on and what the error is pointing to. REALLY REALLY appreciate your wisdom :)))
"I honestly don't know what's going on and what the error is pointing to. "
The linker error message pretty much tells you what's missing.
The current problem you have, is you're missing a definition for the static InputHandler* s_pInstance; member in a translation unit. It should look like this:
InputHandler* InputHandler::s_pInstance = 0;
Though your approach to the singleton pattern has some flaws and deficiencies (especially regarding thread safety). You should simplify your singleton pattern like this:
static InputHandler& Instance() {
static InputHandler theInputHandler;
return theInputHandler;
}
or if you insist using a a pointer
static InputHandler* Instance() {
static InputHandler theInputHandler;
return &theInputHandler;
}
This is commonly known as 'Scott Meyer's Singleton idiom', here's some more information about the original reasonings;
C++ and the Perils of Double-Checked Locking
The error occurred when I called a constructor of a derived class in another project. I omitted some details in the code. I am using Visual Studio 2012.
-Base/derived classes and the test file are in two different projects. Base/derived classes can be compiled without problems.
-The Test project can be compiled successfully when comment the constructor line.
-Test.cpp plays well with other constructor in the DerivationFunction file.
// Test.cpp
#include "DerivationFunction.h"
Child con(123, 123); // error LNK2019: unresolved external symbol "public: __thiscall Child::Child(unsigned short,unsigned int)" (??Child##QAE#GI#Z) referenced in function _main
The header file of base class and derived class:
// DerivationFunction.h
class Base
{
public:
virtual void AppendEnums() = 0;
static int CopyBuffer();
uint16 GetFeatureID();
protected:
uint16 baseValue;
static int Copy();
};
// Child class
class Child : public Base
{
public:
uint32 childValue;
Child(uint16 featureID, uint32 value);
void AppendEnums();
};
The source file:
// DerivationFunction.cpp
int Base::CopyBuffer()
{
return 0;
}
uint16 Base::GetFeatureID()
{
return baseValue;
}
int Base::Copy()
{
return 0;
}
// Child class
Child::Child(uint16 featureID, uint32 value)
{
baseValue = featureID;
childValue = value;
}
void Child::AppendEnums()
{
}
The simplest answer is that you haven't built and included the implementation file in with your main and hence the linker cannot find the code for the Child constructor. Look in the MSDN help for that particular error code and check all of the possibilities there.
If you want to use these classes in another project then you either include the whole sources (headers and cpp files) and build them, or export them from a DLL project and import them in the other project(s).
I've recently started to program in C++ again, and for the purposes of education, I am working on creating a poker game. The weird part is, I keep getting the following error:
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::Poker(void)" (??0Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic initializer for 'pokerGame''(void)" (??__EpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: __thiscall PokerGame::Poker::~Poker(void)" (??1Poker#PokerGame##QAE#XZ) referenced in function "void __cdecl `dynamic atexit destructor for 'pokerGame''(void)" (??__FpokerGame##YAXXZ)
1>LearningLanguage01.obj : error LNK2019: unresolved external symbol "public: void __thiscall PokerGame::Poker::begin(void)" (?begin#Poker#PokerGame##QAEXXZ) referenced in function _wmain
1>C:\Visual Studio 2012\Projects\LearningLanguage01\Debug\LearningLanguage01.exe : fatal error LNK1120: 3 unresolved externals
I have done some research on the issue, and most point to the constructor and destructor definition in the header and .cpp not matching. I don't see any issues with the header and .cpp.
Here is the code for poker.h:
#pragma once
#include "Deck.h"
using namespace CardDeck;
namespace PokerGame
{
const int MAX_HAND_SIZE = 5;
struct HAND
{
public:
CARD cards[MAX_HAND_SIZE];
};
class Poker
{
public:
Poker(void);
~Poker(void);
HAND drawHand(int gameMode);
void begin();
};
}
And the code in the .cpp:
#include "stdafx.h"
#include "Poker.h"
using namespace PokerGame;
const int TEXAS_HOLDEM = 0;
const int FIVE_CARD = 1;
class Poker
{
private:
Deck deck;
Poker::Poker()
{
deck = Deck();
}
Poker::~Poker()
{
}
void Poker::begin()
{
deck.shuffle();
}
//Draws a hand of cards and returns it to the player
HAND Poker::drawHand(int gameMode)
{
HAND hand;
if(gameMode == TEXAS_HOLDEM)
{
for(int i = 0; i < sizeof(hand.cards); i++)
{
hand.cards[i] = deck.drawCard();
}
}
return hand;
}
};
Because of the comment below, I've rewritten what I had before.
The problem that the linker is complaining about is that you've declared your member functions in Poker, but haven't defined them. How is this? For starters, you're creating a new class and defining separate member functions in it.
Your header file Poker class exists in the PokerGame namespace and your cpp file Poker class exists in the global namespace. To fix that issue, put them in the same namespace:
//cpp file
namespace PokerGame {
class Poker {
...
};
}
Now that they're in the same namespace, you have another issue. You're defining your member functions inside the class body, but not the first one. The definitions simply can't go in the body of a class named the same way. Get rid of the whole class in the cpp file:
//cpp file
namespace PokerGame {
Poker::Poker() {
deck = Deck(); //consider a member initializer instead
}
//other definitions
}
One last thing: you put the private section of your class in the wrong spot. It was in that cpp file class that we just removed. It belongs with the other parts of your class:
//header file
namespace PokerGame {
class Poker {
public:
//public stuff
private:
Deck deck; //moved from cpp file
};
}
Another solution could be: check the cmake file and make sure it (such as in ADD_EXECUTABLE) includes the .cpp file you listed.
class CommandManager {
public:
void sendText(std::string command);
static bool CommandManager::started;
private:
bool parseCommand(std::string commands);
void changeSpeed(std::vector<std::string> vec);
void help(std::vector<std::string> vec);
};
And here's the client code:
CommandManager::started = true;
Linking these two files together I get:
1>UAlbertaBotModule.obj : error LNK2001: unresolved external symbol "public: static bool CommandManager::started" (?started#CommandManager##2_NA)
1>C:\Development\School\cmput350-uofabot\UAlbertaBot\vs2008\Release\UAlbertaBot.dll : fatal error LNK1120: 1 unresolved externals
Where did I go wrong here?
You're doing that incorrectly.
class CommandManager {
public:
void sendText(std::string command);
static bool started; //NOT this -> bool CommandManager::started
//...
};
then put the definition of static member in .cpp file as:
#include "CommandManager.h" //or whatever it is
bool CommandManager::started = true; //you must do this in .cpp file
Now you can use CommandManager::started in your client code.
You should have inside your class:
class CommandManager {
public:
void sendText(std::string command);
static bool started;
//// etc
};
and outside your class, in a *.cc file (not in a *.hh header file), a definition like
bool CommandManager::started;
BTW, I believe you'll better make that private.
Consider putting
bool CommandManager::started;
where you define other members.
Ok, I have defined the template class, which compiles as expected, when I implement this class in a function of the CMainFrame of the application and compile it, I receive unresolved linking errors.
void CMainFrame::OnFunc()
{
CTestList<CMyClass> list;
}
The linking errors:
1>mainfrm.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CTestList<class CMyClass>::~CTestList<class CMyClass>(void)" (??1?$CTestList#VCWnd####UAE#XZ) referenced in function "protected: void __thiscall CMainFrame::OnFunc(void)" (?OnFunc#CMainFrame##IAEXXZ)
1>mainfrm.obj : error LNK2019: unresolved external symbol "public: __thiscall CTestList<class CMyClass>::CTestList<class CMyClass>(void)" (??0?$CTestList#VCWnd####QAE#XZ) referenced in function "protected: void __thiscall CMainFrame::OnFunc(void)" (?OnFunc#CMainFrame##IAEXXZ)
I've checked all the obvious missing headers, undefined functions, etc, but still it throws these errors at me, the files are all part of the main application and are not in static/shared libs, as this is the error I would expect if i'd done so..
Here is the basic definition of the template class cut right down, I've followed what I believe to be the correct path in constructing the class, and all my research seems to suggest its correct.
Really need to get this nailed ASAP, so if you guys & girls could help I would be very grateful.
Cheers,
DIGGIDY
/////////////////////////////////////////////////////////////////////////////
// CTestList class
template <class T>
class CTestList : public CMDIChildWndEx
{
//DECLARE_DYNAMIC(CTestList<T>)
public:
CTestList();
virtual ~CTestList();
protected:
// Generated message map functions
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
// CTestList
//IMPLEMENT_DYNCREATE(CTestList<SDCM_OBJECT_TYPE>, CMDIChildWndEx)
template <class T>
CTestList<T>::CTestList()
{
}
template <class T>
CTestList<T>::~CTestList()
{
}
BEGIN_TEMPLATE_MESSAGE_MAP(CTestList, T, CMDIChildWndEx)
ON_WM_CREATE()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTestList message handlers
template <class T>
int CTestList<T>::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if ( CMDIChildWndEx::OnCreate(lpCreateStruct) == -1 )
return -1;
// this removes the crappy un-drawn client edge on screen
ModifyStyleEx(WS_EX_OVERLAPPEDWINDOW, WS_EX_WINDOWEDGE);
return 0;
}
Your template code is not inlined in the header file. When the template class cpp file is being compiled the compiler doesn't know what instances of T will be required. When your main file is being compiled and you need to instantiate a CTestList the compiler only has the template header file. You need to add a force explicite template instantiation to your template .cpp file - so at the moment this is compiled it will generation the correct CMyClass instantiation of the template.