Singleton Implemenatatio Linker Error - visual-c++

class Game
{
public:
//! Returns the only instance of the game object
static shared_ptr<Game> GetGameInstance();
//! Function with continous loop for game play
void GamePlay();
//! The hidden constructor
~Game();
private:
//! The hidden constructor
Game();
static shared_ptr<Game> _game;
};
//.cpp file
#include "Game.h"
shared_ptr<Game> Game::_game;
shared_ptr<Game> Game::GetGameInstance()
{
if(_game == NULL)
{
_game.reset(new Game);
}
return _game;
}
void Game::GamePlay()
{
shared_ptr<Graphics> myGameGraphics;
while(!myGameGraphics->UserForcedExit())
{
myGameGraphics->drawMaze();
}
}
Game::~Game()
{
}
I have the code above,but the compilation gives me a linker error:
Game.obj : error LNK2019: unresolved external symbol "private: __thiscall Game::Game(void)" (??0Game##AAE#XZ) referenced in function "public: static class boost::shared_ptr __cdecl Game::GetGameInstance(void)" (?GetGameInstance#Game##SA?AV?$shared_ptr#VGame###boost##XZ)
Can anyone please help...

try this:
shared_ptr<Game> Game::GetGameInstance()
{
if(_game = NULL)
_game = shared_ptr<Game>(new Game);
return _game;
}

Related

C++ / CLI linker error because of SHAssocEnumHandlers function

To reproduce the issue,
Create a project > VC++ > CLR > Class library, name the project "ClassLibrary2" and use below two files as is.
// ClassLibrary2.h
#pragma once
#include <msclr\marshal.h>
#include <ShObjIdl.h>
using namespace System;
namespace ClassLibrary2 {
public ref class Class1
{
public : void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext);
};
}
// ClassLibrary2.cpp
// This is the main DLL file.
#include "stdafx.h"
#include "ClassLibrary2.h"
void ClassLibrary2::Class1::GetAllOpenWithProgs(String^ ext)
{
msclr::interop::marshal_context ^ context = gcnew msclr::interop::marshal_context();
PCWSTR pszStr = context->marshal_as<const wchar_t*>(ext);
IEnumAssocHandlers *pEnumHandlers = NULL;
/* below line is producing problems */
SHAssocEnumHandlers(pszStr, ASSOC_FILTER_RECOMMENDED, &pEnumHandlers);
}
Now, if I build the project, I get following errors :
Error 2 error LNK2028: unresolved token (0A0000A2) "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
Error 3 error LNK2019: unresolved external symbol "extern "C" long
__cdecl SHAssocEnumHandlers(wchar_t const *,enum ASSOC_FILTER,struct IEnumAssocHandlers * *)"
(?SHAssocEnumHandlers##$$J0YAJPEB_WW4ASSOC_FILTER##PEAPEAUIEnumAssocHandlers###Z)
referenced in function "public: void __clrcall
ClassLibrary2::Class1::GetAllOpenWithProgs(class System::String ^)"
(?GetAllOpenWithProgs#Class1#ClassLibrary2##$$FQE$AAMXPE$AAVString#System###Z) C:\Users\Anjum\Documents\Visual
Studio
2012\Projects\WpfApplication2\ClassLibrary2\ClassLibrary2.obj ClassLibrary2
How to remove those linker errors ?

C++: Migrating dll from VS 2008 to VS 2010: error C2678

I'm migrating the code of an dll, developed with C++, from Visual Studio version 2008 to 2010.
I'm facing this error below which is related to the 'C:\Program Files\Microsoft Visual Studio 10.0\VC\include\xstring'.
Error C2678: binary operator '!=': no operator found which takes a left-hand operand of type 'c_allocator<_Ty>' (or there is no acceptable conversion).
The error occurs with the line 'class c_allocator: public std::_Allocator_base<_Ty>{'.
#include <stdlib.h>
#include <vector>
#include "Synchro.h"
template<class _Ty>
class c_allocator: public std::_Allocator_base<_Ty>{
public:
typedef _Allocator_base<_Ty> _Mybase;
typedef typename _Mybase::value_type value_type;
typedef value_type _FARQ *pointer;
typedef value_type _FARQ& reference;
typedef const value_type _FARQ *const_pointer;
typedef const value_type _FARQ& const_reference;
typedef _SIZT size_type;
typedef _PDFT difference_type;
template<class _Other>struct rebind{
typedef c_allocator<_Other> other;
};
pointer address(reference _Val) const {
return (&_Val);
}
const_pointer address(const_reference _Val) const {
return (&_Val);
}
c_allocator() _THROW0() {
}
c_allocator(const c_allocator<_Ty>&) _THROW0() {
}
template<class _Other>c_allocator(const c_allocator<_Other>&) _THROW0() {
}
template<class _Other>c_allocator<_Ty>& operator=(const c_allocator<_Other>&) {
return (*this);
}
void deallocate(pointer _Ptr, size_type) {
CustomAlloc::LinearAllocHolder<1>::alloc.freemem(_Ptr);
}
pointer allocate(size_type _Count) {
return (pointer)CustomAlloc::LinearAllocHolder<1>::alloc.getmem(_Count*sizeof(value_type));
}
pointer allocate(size_type _Count, const void _FARQ *) {
return (allocate(_Count));
}
void construct(pointer _Ptr, const _Ty& _Val) {
std::_Construct(_Ptr, _Val);
}
void destroy(pointer _Ptr) {
std::_Destroy(_Ptr);
}
_SIZT max_size() const _THROW0() {
_SIZT _Count = (_SIZT)(-1) / sizeof (_Ty);
return (0 < _Count ? _Count : 1);
}
};
Any hint much appreciated -
thanks in advance -
surplus

error LNK2019: unresolved external symbol/

I'm having a lot of trouble trying to compile an ogre sample found on Github.
I've had several Intellisense errors, compilation & linking errors. Now I'm stuck with 2 linker errors. I know there's a lot of similar questions around here because I've read a lot on the subject but I can't find (or see) the right solution.
error LNK2019: unresolved external symbol "public: __thiscall NFSpace::PlanetMapTile::PlanetMapTile(struct NFSpace::QuadTreeNode *,class Ogre::SharedPtr<class Ogre::Texture>,class Ogre::Image,class Ogre::SharedPtr<class Ogre::Texture>,int)" (??0PlanetMapTile#NFSpace##QAE#PAUQuadTreeNode#1#V?$SharedPtr#VTexture#Ogre###Ogre##VImage#4#1H#Z) referenced in function "public: class NFSpace::PlanetMapTile * __thiscall NFSpace::PlanetMap::finalizeTile(struct NFSpace::QuadTreeNode *)" (?finalizeTile#PlanetMap#NFSpace##QAEPAVPlanetMapTile#2#PAUQuadTreeNode#2##Z)
error LNK2019: unresolved external symbol "public: struct NFSpace::QuadTreeNode const * __thiscall NFSpace::PlanetMapTile::getNode(void)" (?getNode#PlanetMapTile#NFSpace##QAEPBUQuadTreeNode#2#XZ) referenced in function "public: void __thiscall NFSpace::PlanetRenderable::setFrameOfReference(struct NFSpace::PlanetLODConfiguration &)" (?setFrameOfReference#PlanetRenderable#NFSpace##QAEXAAUPlanetLODConfiguration#2##Z)
here is the code associated with the first error:
PlanetMapTile.h
namespace NFSpace {
class PlanetMapTile {
public:
PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size);
~PlanetMapTile();
};
}
PlanetMapTile.cpp
#include "PlanetMapTile.h"
namespace NFSpace {
PlanetMapTile::PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size) {
//do something
}
PlanetMapTile::~PlanetMapTile() {
//do something
}
}
PlanetMap.h
#include "PlanetMapTile.h"
namespace NFSpace {
class PlanetMap {
public:
PlanetMapTile* finalizeTile(QuadTreeNode* node);
};
}
PlanetMap.cpp
#include "PlanetMap.h"
namespace NFSpace {
PlanetMapTile* PlanetMap::finalizeTile(QuadTreeNode* node) {
mStep = 0;
return new PlanetMapTile(node, mHeightTexture, mHeightImage, mNormalTexture, getInt("planet.textureSize"));
}
}
Any help would be appreciated.
That is how you should declare the name space
PlanetMApTile.h
namespace NFSpace{
class PlanetMapTile {
public:
PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size);
~PlanetMapTile();
};
}
PlanetMapTile.cpp
#include "PlanetMapTile.h"
NFSpace::PlanetMapTile::PlanetMapTile(QuadTreeNode* node, TexturePtr heightTexture, Image heightImage, TexturePtr normalTexture, int size) {
//do something
}
NFSpace::PlanetMapTile::~PlanetMapTile() {
//do something
}
PlanetMap.h
#include "PlanetMapTile.h"
class PlanetMap {
public:
NFSpace::PlanetMapTile* finalizeTile(QuadTreeNode* node);
}
PlanetMap.cpp
#include "PlanetMap.h"
NFSpace::PlanetMapTile* PlanetMap::finalizeTile(QuadTreeNode* node) {
mStep = 0;
return new NFSpace::PlanetMapTile(node, mHeightTexture, mHeightImage, mNormalTexture, getInt("planet.textureSize"));
}
So I've finally found the solution:
Considering the fact that PlanetMapTile() and getNode() were both involving QuadTreeNode* and that ~PlanetMapTile() didn't raise an error, I've started to look at the QuadTreeNode declaration which is located in PlanetCubeTree.h. Then I've just tried to add #include "PlanetCubeTree.h" to PlanetMapTile.h and it solved the error.
Thank you for your help

LNK2019 & LNK1120 Unresolved Externals Probably an easy fix, but I'm having real trouble

I've been getting these LNK2019s for a little while now and can't seem to find a way to get rid of them. I'm aware that there are a lot of threads on these errors already, but I've yet to find anything that's helped me so hoped someone might miss something glaringly obvious I may have missed.
I've not learnt very traditionally, so sorry if my code's a bit messy.
main:
#include "eventLoop.h"
#include <time.h>
#include <iostream>
using namespace std;
bool buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG = false;
bool KeepProgramOpen = true;
time_t timer;
time_t oldtime;
time_t dtime;
time_t looptime;
int rate;
char Note;
bool FirstLoop = true;
eventLoop MainEventLoop;
int main()
{
rate = 60;
looptime = 1000 / rate;
while(KeepProgramOpen==true)
{
time(&timer);
dtime = timer-oldtime;
if(dtime<looptime)
{
continue;
}
oldtime = timer;
MainEventLoop.FindFinalNote(buttonA, buttonB, buttonC, buttonD, buttonE, buttonF, buttonG, FirstLoop);
FirstLoop = false;
//Closing stuff goes here
}
}
eventLoop.h:
#pragma once
class eventLoop {
public:
void FindFinalNote(bool, bool, bool, bool, bool, bool, bool, bool);
protected:
};
eventLoop.cpp:
#include "eventLoop.h"
#include "MidiOutput.h"
#include "FileIO.h"
MidiOutput MidiOutputX;
FileIO fileioX;
void eventLoop::FindFinalNote(bool A, bool B, bool C, bool D, bool E, bool F, bool G, bool firstloop)
{
if(firstloop == true)
{
for (int loopindex=0; loopindex<10; loopindex++)
{
// Note[loopindex] = Filecheck for notes
}
MidiOutputX.FindDevice(
1, /*int argc number of ports*/
60, /*char argv argument vector - strings pointed to, i don't really get it*/
);
}
char Note[10];
int KeyIndex = 0;
FileIO::key CurrentKey;
CurrentKey = fileioX.RetrieveKey(KeyIndex);
for (int x = 0; x < 10; x++)
{
Note[x] = CurrentKey.Note[x];
}
// There's a bunch of simple if statements here, nothing I need to bore you with
}
MidiOutput.h:
#pragma once
class MidiOutput {
public:
void FindDevice(int, char);
void PlayNote(unsigned char);
void EndNote();
void CloseDevice();
protected:
};
MidiOutput.cpp:
#include "MidiOutput.h"
#include <Windows.h>
#include <mmsystem.h>
#include <stdio.h>
union { unsigned long word; unsigned char data[4]; } message;
int midiport;
HMIDIOUT device;
void FindDevice(int argc, char** argv)
{
if (argc < 2) {
midiport = 0;
} else {
midiport = atoi(argv[1]);
}
printf("Midi output port set to %d.\n", midiport);
midiOutOpen(&device, midiport, 0, 0, CALLBACK_NULL);
message.data[0] = 0x90; //command byte
message.data[1] = 60; //middle C
message.data[2] = 0; //volume, 0-100
message.data[3] = 0; //not used
}
void MidiOutput::PlayNote(unsigned char Note)
{
message.data[1] = Note;
message.data[2] = 100;
}
void MidiOutput::EndNote()
{
message.data[2] = 0;
}
void MidiOutput::CloseDevice()
{
midiOutReset(device);
midiOutClose(device);
}
Exact Errors:
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall MidiOutput::FindDevice(int,char)" (?FindDevice#MidiOutput##QAEXHD#Z) referenced in function "public: void __thiscall eventLoop::FindFinalNote(bool,bool,bool,bool,bool,bool,bool,bool)" (?FindFinalNote#eventLoop##QAEX_N0000000#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\eventLoop.obj GHX
Error 2 error LNK2019: unresolved external symbol _imp_midiOutOpen#20 referenced in function "void __cdecl FindDevice(int,char * *)" (?FindDevice##YAXHPAPAD#Z) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 3 error LNK2019: unresolved external symbol _imp_midiOutClose#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 4 error LNK2019: unresolved external symbol _imp_midiOutReset#4 referenced in function "public: void __thiscall MidiOutput::CloseDevice(void)" (?CloseDevice#MidiOutput##QAEXXZ) C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\GHX\MidiOutput.obj GHX
Error 5 error LNK1120: 4 unresolved externals C:\Users\Hilly\documents\visual studio 2010\Projects\GHX\Debug\GHX.exe GHX
Thanks in advance, and sorry about the wall of code, I'm not sure what's necessary.
The missing symbols, midiOutOpen, midiOutClose etc. are defined in the DLL Winmm.dll. You will need to link to Winmm.lib by either specifying it as an input to the link command or by including this in your file:
#pragma comment(lib, "Winmm.lib")
You're also getting an error about MidiOutput::FindDevice. You will need to fix up the signatures so that the .h file and .cpp match, and qualify the function definition in the .cpp file with the class name (MidiOutput::).

Singleton code linker errors in vc 9.0. Runs fine in linux compiled with gcc

I have a simple logger that is implemented as a singleton. It works like i want when I compile and run it with g++ in linux but when I compile in Visual Studio 9.0 with vc++ I get the following errors. Is there a way to fix this? I don't mind changing the logger class around, but I would like to avoid changing how it is called.
1>Linking...
1>loggerTest.obj : error LNK2005: "public: static class Logger * __cdecl Logger::getInstance(void)" (?getInstance#Logger##SAPAV1#XZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::log(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?log#Logger##QAEXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "public: void __thiscall Logger::closeLog(void)" (?closeLog#Logger##QAEXXZ) already defined in Logger.obj
1>loggerTest.obj : error LNK2005: "private: static class Logger * Logger::_instance" (?_instance#Logger##0PAV1#A) already defined in Logger.obj
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Logger::_path" (?_path#Logger##0V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class boost::mutex Logger::_mutex" (?_mutex#Logger##0Vmutex#boost##A)
1>Logger.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
1>loggerTest.obj : error LNK2001: unresolved external symbol "private: static class std::basic_ofstream<char,struct std::char_traits<char> > Logger::_log" (?_log#Logger##0V?$basic_ofstream#DU?$char_traits#D#std###std##A)
The code, three files Logger.h Logger.cpp test.cpp
#ifndef __LOGGER_CPP__
#define __LOGGER_CPP__
#include "Logger.h"
Logger* Logger::_instance = 0;
//string Logger::_path = "log";
//ofstream Logger::_log;
//boost::mutex Logger::_mutex;
Logger* Logger::getInstance(){
{
boost::mutex::scoped_lock lock(_mutex);
if(_instance == 0) {
_instance = new Logger;
_path = "log";
}
} //mutex
return _instance;
}
void Logger::log(const std::string& msg){
{
boost::mutex::scoped_lock lock(_mutex);
if(!_log.is_open()){
_log.open(_path.c_str());
}
if(_log.is_open()){
_log << msg.c_str() << std::endl;
}
}
}
void Logger::closeLog(){
Logger::_log.close();
}
#endif
` ...
#ifndef __LOGGER_H__
#define __LOGGER_H__
#include <iostream>
#include <string>
#include <fstream>
#include <boost/thread/mutex.hpp>
#include <boost/thread.hpp>
using namespace std;
class Logger {
public:
static Logger* getInstance();
void log(const std::string& msg);
void closeLog();
protected:
Logger(){}
private:
static Logger* _instance;
static string _path;
static bool _logOpen;
static ofstream _log;
static boost::mutex _mutex; //check mutable
};
#endif
test.cpp
`
#include <iostream>
#include "Logger.cpp"
using namespace std;
int main(int argc, char *argv[])
{
Logger* log = Logger::getInstance();
log->log("hello world\n");
return 0;
}
This is a problem because you've defined the symbol multiple times by compiling the CPP then also including it in test.cpp... By convention you should only include the declaration, not the definition as you've done.
I am surprised that gcc would allow one to be so lax about this.
change
#include "Logger.cpp"
to
#include "Logger.h"
and give it a try.
The problem is because you have done #include "Logger.cpp" instead of #include "Logger.h" in test.cpp . Because of this, the symbols inside the Logger.cpp will be defined multiple times (once for the translation unit logger.cpp and once for test.cpp). Having multiple inclusion guard doesn't help because it works only within a translation unit.

Resources