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.
Related
//Baseclass.h
class Baseclass {
private:
uint8_t index;
public:
Baseclass(uint8_t index);
}
//Baseclass.cpp
#include "Baseclass.h"
Baseclass::Baseclass(uint8_t index) {
index = index;
};
//Subclass.h
#include "Baseclass.h"
class Subclass : public Baseclass {
public:
Subclass();
};
//Subclass.cpp
#include "Subclass.h"
#include "Baseclass.h"
Subclass::Subclass() : Baseclass(0) {};
What am I missing? I kept getting LNK2019 Error
Severity Code Description Project File Line Suppression State
Error
LNK2019 unresolved external symbol "public: __thiscall Baseclass::Baseclass(unsigned char)" (??Baseclass##QAE#E#Z) referenced in function "public: __thiscall Subclass::Subclass(void)" (??Subclass##QAE#XZ)
It couldn't link Baseclass constructor. Are you sure there are no issues with compiling it? If you copy pasted all of the code you lack semicolon at the end of baseclass.
I am looking for the source of the error since a few hours without success. My project consists of two sub projects. The first one is a dll and the second one is an application (exe).
I simplified my original code, which is part of the dll:
#ifndef blub_base
#define blub_base
#include "SomeInterface.hpp"
#include "Object.hpp"
#include <map>
namespace a
{
namespace b
{
class __declspec(dllexport) CBase : public CSomeInterface
{
protected:
CBase() {}
public:
CBase(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
~CBase() { /* concrete implementation in cpp */ }
bool initialize() { /* concrete implementation in cpp */ }
bool shutdown() { /* concrete implementation in cpp */ }
void foo() { /* concrete implementation in cpp */ }
virtual void blubblub(Object* f_object_p) { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const { /* concrete implementation in cpp */ }
};
}
}
#endif
#ifndef blub
#define blub
#include "Base.hpp"
namespace a
{
namespace b
{
class __declspec(dllexport) CChild : public CBase
{
private:
CChild() {}
public:
CChild(SomeDifferentObject* f_object_p) { /* concrete implementation in cpp */ }
/// deconstructor
~CChild() { /* concrete implementation in cpp */ }
void blubblub(Object* f_object_p) override { /* concrete implementation in cpp */ }
protected:
bool blub1(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
bool blub2(Object* f_object_p, std::map<uint32_t, Object*>& f_something_rmap) const override { /* concrete implementation in cpp */ }
void blub3(Object* f_object_p) const override { /* concrete implementation in cpp */ }
};
}
}
#endif
If I am trying to instantiate a CChild object in my application I get linker errors for all functions of the CChild class:
Error 75 error LNK2001: unresolved external symbol "public: virtual void __thiscall a::b::CChild::blubblub(class a::b::Object *)" (?blubblub#CChild#b#a##UAEXPAVObject#23##Z) application.obj Application
Error 74 error LNK2019: unresolved external symbol "public: virtual __thiscall a::b::CChild::~CChild(void)" (??1CChild#b#a##UAE#XZ) referenced in function "public: virtual void * __thiscall a::b::CChild::`vector deleting destructor'(unsigned int)" (??_ECChild#b#a##UAEPAXI#Z) Application.obj Application
Error 73 error LNK2019: unresolved external symbol "public: __thiscall a::b::CChild::CChild(class a::b::SomeDifferentObject *)" (??0CChild#b#a##QAE#PAVSomeDifferentObject#12##Z) referenced in function _main Application.obj Application
Error 77 error LNK2001: unresolved external symbol "protected: virtual bool __thiscall a::b::CChild::blub1(class Object *,class std::map,class std::allocator > > &)const " (?blub1#CChild#b#a##MBE_NPAVObject#23#AAV?$map#KIU?$less#K#std##V?$allocator#U?$pair#$$CBKI#std###2##std###Z) Application.obj Application
Error 76 error LNK2001: unresolved external symbol "protected: virtual bool __thiscall a::b::CChild::blub2(class Object *,class std::map,class std::allocator > > &)const " (?blub2#CChild#b#a##MBE_NPAVObject#23#AAV?$map#KIU?$less#K#std##V?$allocator#U?$pair#$$CBKI#std###2##std###Z) Application.obj Application
Error 78 error LNK2001: unresolved external symbol "protected: virtual void __thiscall a::b::CChild::blub3(class a::b::Object *)const " (?blub3#CChild#b#a##MBEXPAVObject#23##Z) Application.obj Application
I am using Visual Studio and all cpp files are in the project (checked many times). Each function is implemented e.g.
CChild::CChild(SomeDifferentObject* f_object_p) : CBase(f_object_p)
{
}
It seems that the relevant cpp files are not found?!
Thank you very much for your help!
Kind regards,
Bobby
It is not working because classes are always exported. They need to be exported by dll project and imported by projects that use that dll.
To fix this, add header file, for example ab_dll.h and there:
#ifdef AB_DLL_EXPORT
#define AB_DLL_API __declspec(dllexport)
#else
#define AB_DLL_API __declspec(dllimport)
#endif
Then use that macro in your classes:
class AB_DLL_API CBase : public CSomeInterface
{
//...
};
class AB_DLL_API CChild : public CBase
{
//...
};
Also in your VS dll project add AB_DLL_EXPORT in PreprocessorDefinitions so that the classes are exported. This works that way that if AB_DLL_EXPORT is defined in the project then classes will be exported, otherwise will be imported.
I am trying to implement a PointArray class derived from a template. Here is what my hpp file for PointArray looks like:
#ifndef POINTARRAY_H
#define POINTARRAY_H
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "Array.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
class PointArray : public Array<Point>
{
public:
PointArray();//Default constrcutor.
PointArray(int size);//Constructor with size argument.
PointArray(const PointArray& arr);//Copy constructor.
~PointArray();//Destructor.
double Length() const;//Length function.
};
}
}
#endif
My cpp looks like this :
#include <iostream>
#include <sstream>
#include <stdio.h>
#include "PointArray.hpp"
using namespace Abhishek::CAD;
using namespace Abhishek::CONTAINERS;
namespace Abhishek
{
namespace CONTAINERS
{
//Default constructor.
PointArray::PointArray(): Array<Point>()
{
cout<<"Point arr default cons"<<endl;
}
//Constructor with size argument.
PointArray::PointArray(int size) : Array<Point>(size)
{
}
//Copy constructor.
PointArray::PointArray(const PointArray& arr) : Array<Point>(arr)
{
}
//destrcutor.
PointArray::~PointArray()
{
}
}
}
I get the LNK error :
error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(void)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(void)" (??0PointArray#CONTAINERS#Abhishek##QAE#XZ)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(int)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#H#Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(int)" (??0PointArray#CONTAINERS#Abhishek##QAE#H#Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::Array<class Abhishek::CAD::Point>(class Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point> const &)" (??0?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#ABV012##Z) referenced in function "public: __thiscall Abhishek::CONTAINERS::PointArray::PointArray(class Abhishek::CONTAINERS::PointArray const &)" (??0PointArray#CONTAINERS#Abhishek##QAE#ABV012##Z)
1>PointArray.obj : error LNK2019: unresolved external symbol "public: __thiscall Abhishek::CONTAINERS::Array<class Abhishek::CAD::Point>::~Array<class Abhishek::CAD::Point>(void)" (??1?$Array#VPoint#CAD#Abhishek###CONTAINERS#Abhishek##QAE#XZ) referenced in function __unwindfunclet$??0PointArray#CONTAINERS#Abhishek##QAE#XZ$0
1>C:\Users\Rambo\Documents\Level 6\Section 4.2b\Exercise 3\Debug\Exercise 3.exe : fatal error LNK1120: 4 unresolved externals
I dont understand why this could be happening. I included all the relevant header files and CPP files. If anyone can help I will really appreciate it.
You forgot to post the most relevant header: the one that declares the class template that causes the error. Almost certainly, that header declares the default constructor of the Array template:
Array();
but doesn't define it; either there is no definition, or you have a definition in a source file.
In either case, you'll get an error since templates must be defined in any translation unit that uses them. This means that you'll need to define the constructor (and any other member functions) in the header, to include them wherever they are used.
If that's not the problem, then please post the header so we can investigate further.
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).
This seems like a problem that is common. I defined an enum in classA and then included classA in classB. Then, in classB I defined a function which returns the enum type defined in classA...see below. I get the following error:
aFirst.obj : error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)" (?returnsThings#usesTheEnum##QAE?AW4things_t#justEnum##XZ) referenced in function _wmain
1>C:\Documents and Settings\Ben\My Documents\Visual Studio 2010\Projects\aFirst\Debug\aFirst.exe : fatal error LNK1120: 1 unresolved externals
#pragma once
class justEnum
{
public:
justEnum(void);
~justEnum(void);
enum things_t{ONE, TWO};
};
#pragma once
#include "justEnum.h"
class usesTheEnum
{
public:
usesTheEnum(void);
~usesTheEnum(void);
justEnum::things_t returnsThings(void);
};
#include "StdAfx.h"
#include "usesTheEnum.h"
#include "justEnum.h"
usesTheEnum::usesTheEnum(void)
{
}
usesTheEnum::~usesTheEnum(void)
{
}
justEnum::things_t returnsThings()
{
return justEnum::ONE;
}
// tester.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include "justEnum.h"
#include "usesTheEnum.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
usesTheEnum aUser;
justEnum anEnum;
justEnum::things_t anotherEnum;
anotherEnum = justEnum::ONE;
aUser.returnsThings();
cout << anotherEnum;
return 0;
}
You need to specify that your definition of returnsThings() is part of the usesTheEnum class.
justEnum::things_t usesTheEnum::returnsThings()
{
return justEnum::ONE;
}
error LNK2019: unresolved external symbol "public: enum justEnum::things_t __thiscall usesTheEnum::returnsThings(void)"
The compiler is complaining that usesTheEnum::returnThings() is not defined, and I cannot see a definition in the code you posted. You should provide a definition for the function in one translation unit.
I don't think I can emphasize enough how important it to learn to read error messages. The compiler is doing it's best to tell you what is wrong.
not shure, but can't you just move the enum out of the class?
Or on the .cpp of the class write something like
extern enum classname::things_t;
just to have the enum added to the generated lib wich is what will be linked against.