error LNK2019: unresolved external symbol in derived class - c++

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).

Related

C++ inheritence and LNK2019

I have got this problem and don't know how to solve it.
I have two classes. One is base ProgramVariableBase and one is derived ProgramVariable. Base class is in different project that is bulid as static library. I set additional include and library directories in properties of project with derived class. I am using VS2012.
// ProgramVariableBase.h
#include <string>
class ProgramVariableBase
{
protected:
std::string m_name;
bool m_initialized;
public:
ProgramVariableBase(const char* name);
virtual ~ProgramVariableBase();
virtual const std::string & Name();
virtual void MakeInitialized();
};
// ProgramVariableBase.cpp
#include "ProgramVariableBase.h"
ProgramVariableBase::ProgramVariableBase(const char* name)
{
m_name = name;
m_initialized = false;
}
ProgramVariableBase::~ProgramVariableBase()
{
}
const std::string & ProgramVariableBase::Name()
{
return m_name;
}
void ProgramVariableBase::MakeInitialized()
{
m_initialized = true;
}
// ProgramVariable.h
#include "ProgramComponents\ProgramVariableBase.h"
class ProgramVariable : public ProgramVariableBase
{
public:
ProgramVariable(const char* name);
~ProgramVariable();
void MakeInitialized() override;
};
// ProgramVariable.cpp
#include "ProgramVariable.h"
ProgramVariable::ProgramVariable(const char* name) : ProgramVariableBase(name)
{
}
ProgramVariable::~ProgramVariable()
{
}
void ProgramVariable::MakeInitialized()
{
m_initialized = true;
}
I can build project with base class with no problem, but I get errors when I try build project with derived class. Every error looks similar and concerns methods from base class, its constructor and destructor. I looked for similar answers here, but no one helped. Am I doing something wrong or is my project properties incorrect?
unresolved external symbol "public: __thiscall ProgramVariableBase::ProgramVariableBase(char const *)" (??0ProgramVariableBase##QAE#PBD#Z) referenced in function "public: __thiscall ProgramVariable::ProgramVariable(char const *)"
unresolved external symbol "public: virtual __thiscall ProgramVariableBase::~ProgramVariableBase(void)" (??1ProgramVariableBase##UAE#XZ) referenced in function "public: virtual __thiscall ProgramVariable::~ProgramVariable(void)"
You need to add the library to your linker settings. Just putting the folder containing the library into your additional library settings is not sufficient.
You can add the library to your linker settings via Linker->Input->Additional Dependencies or a use a pragma in your c++ code.
#pragma comment(lib, "mylib.lib")
Perhaps you didn't add library to additional dependencies list (list of libraries, that actually get linked).
Go to Project Properties -> Linker -> Input -> Additional Dependencies and add name of your library there.

Why doesn't this Inline header compile?

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;

linker error (unresolved symbol) with template class in DLL

I get a linker error - unresolved symbol - when using a (specialized) template class from a DLL (Visual Studio 2008 compiler). I tried to use the 'explicit template instantiation' trick described also here in Stackoverflow, but it didn't work. I broke it down to a very simple reproducable example:
I have a dynamic library (DLL) 'MyTemplates.lib' with a header file 'MyTemplates.h' (and a source file 'MyTemplates.cpp' without any code which simply includes this header file) with the following content:
template <class T>
class A
{
public:
A()
{ int x = 7; }
};
template <class T>
class B : public A<T>
{
public:
B()
{}
};
// do explicit template instantiation for classes A<int> and B<int>
// macro 'MYTEMPLATES_API' is defined in the usual way as:
//#ifdef MYTEMPLATES_EXPORTS
// #define MYTEMPLATES_API __declspec( dllexport )
//#else
// #define MYTEMPLATES_API __declspec(dllimport)
//#endif
template class MYTEMPLATES_API A<int>;
template class MYTEMPLATES_API B<int>;
Now i have another dynamic library 'UserLibary' (which links against 'MyTemplates.lib') with the files 'Util.h' and Util.cpp'. The file 'Util.h' is as follows:
#include "MyTemplates.h"
class UserClass
{
public:
UserClass();
public:
A<int> bla;
B<int> blubb;
};
and the content of the file 'Util.cpp' is:
#include "Util.h"
UserClass::UserClass()
{
}
The problem is now that my library 'UserLibrary' does compile well, but it gives two linker errors as follows:
1>Util.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall B<int>::B<int>(void)" (__imp_??0?$B#H##QAE#XZ) referenced in function "public: __thiscall UserClass::UserClass(void)" (??0UserClass##QAE#XZ)
1>Util.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall A<int>::A<int>(void)" (__imp_??0?$A#H##QAE#XZ) referenced in function "public: __thiscall UserClass::UserClass(void)" (??0UserClass##QAE#XZ)
So the linker can not find the symbols for the default constructors of classes A<int> and B<int>. Why is this possible, and how can i get rid of these linker errors ? I thought that the explict template instantiation of the class A<int> and B<int> (in file 'MyTemplates.h') would solve this, but unfortunately it doesn't seem to help - or am I using it in the wrong way ? My compiler is Visual Studio 2008, operating system is windows 7 64 bit, and code is compiled in 64bit.

error LNK2019: unresolved external symbol

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.

Link error CString

I'm getting a linker error using CString the error is:
error LNK2001: unresolved external symbol "private: static class ATL::CStringT<wchar_t,class StrTraitMFC<wchar_t,class ATL::ChTraitsCRT<wchar_t> > > CConfiguration::_campaignFolderPath" (?_campaignFolderPath#CConfiguration##0V?$CStringT#_WV?$StrTraitMFC#_WV?$ChTraitsCRT#_W#ATL#####ATL##A)
I have a class which is defined as:
class CConfiguration
{
private:
static CString _campaignFolderPath;
public:
static void Read();
private:
CConfiguration(void);
~CConfiguration(void);
};
Its Read method is defined as:
void CConfiguration::Read()
{
CConfigFile configReader(_T("Config.ini"));
TCHAR temp[1024];
configReader.GetStringValue(_T("Campaigns"), _T("CampaignsFolderPath"), temp);
_campaignFolderPath = temp;
}
Any clues as to what is causing the error? I'm using Visual Studio 2008
You need to instantiate the string, you're just declaring it as static now. Add:
CString CConfiguration::_campaignFolderPath;
in the implementation file.
Do you have an implementation line like the following somewhere?
CString CConfiguration::_campaignFolderPath;