error LNK2019: unresolved external symbol... referenced in function [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
So I'm working on serialization (I have never done it before so this is a first for me) and what I have done is created a base class called serialisable that all my other classes that can serialize can inherent from:
#include <iostream>
class Serializable{
public:
Serializable();
virtual ~Serializable();
virtual void serialize();
virtual void deserialize();
};
I then have a class that inherits from it which is my AbstractChunk class:
#pragma once
#include <iostream>
#include <list>
#include <fstream>
#include "AbstractBlock.h"
#include "Serialisable.h"
using namespace std;
#ifndef ABSTRACTCHUNK_H
#define ABSTRACTCHUNK_H
class AbstractChunk: public Serializable{
public:
AbstractChunk();
AbstractChunk(int x, int y);
~AbstractChunk();
virtual int getXpos();
virtual int getYpos();
virtual bool unload();
void serialize();
void deserialize();
private:
list<AbstractBlock> blocks;
int xpos;
int ypos;
};
#endif
and then the .cpp for my AbstractChunk (I edited out all the non important stuff):
#include "AbstractChunk.h"
void AbstractChunk::serialize(){
ofstream chunkFile;
chunkFile.open("ChunkData/" + to_string(xpos) + "." + to_string(ypos) + ".chunk");
if (!chunkFile.good())
cout << "Problem Opening Chunk File" << xpos << "." << ypos << endl;
chunkFile << "xpos:" << xpos << "\n";
chunkFile << "ypos:" << ypos << "\n";
chunkFile.close();
}
void AbstractChunk::deserialize(){
}
So where is this error coming from? It's a linker error however I didn't mess with the dependencies or the project setup at all, I have a feeling I'm doing something stupid as usual.
EDIT
Here are the actual errors
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Serializable::Serializable(void)" (??0Serializable##QAE#XZ) referenced in function "public: __thiscall AbstractChunk::AbstractChunk(int,int)" (??0AbstractChunk##QAE#HH#Z) C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter
Error 2 error LNK2019: unresolved external symbol "public: virtual __thiscall Serializable::~Serializable(void)" (??1Serializable##UAE#XZ) referenced in function __unwindfunclet$??0AbstractChunk##QAE#HH#Z$0 C:\Users\Magnus\Documents\Visual Studio 2013\Projects\Top Down Shooter\Top Down Shooter\AbstractChunk.obj Top Down Shooter

You are not specifying the exact linking error but for sure you are missing some methods, you have declared:
class Serializable {
..
virtual void serialize();
virtual void deserialize();
}
as non pure virtual methods, and you are not implementing them. You should turn them to pure, since Serializable doesn't implement functionality but it's only an interface:
class Serializable {
..
virtual void serialize() = 0;
virtual void deserialize() = 0;
}

Related

Using Interface class in Qt Object causes linker errors LNK2001

I'm having strange linkage problems with the following very simple application, which has a class inheriting both QObject and an interface class.
#include <QApplication>
#include <memory>
#include <iostream>
#include <qobject>
class IFoo {
public:
virtual ~IFoo() {}
virtual void foo()=0;
};
class Foo: public QObject, public IFoo
{
Q_OBJECT
public:
explicit Foo(QObject *parent=0): QObject(parent) { std::cout << "foo ctor" << std::endl; }
void foo() override { std::cout << "foo::foo" << std::endl; }
};
std::unique_ptr<IFoo> createFoo() { return std::unique_ptr<IFoo>(new Foo()); }
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
auto foo = createFoo();
return a.exec();
}
This causes the following errors:
LNK2001: unresolved external symbol "public: virtual struct QMetaObject const * __cdecl Foo::metaObject(void)const " (?metaObject#Foo##UEBAPEBUQMetaObject##XZ)
LNK2001: unresolved external symbol "public: virtual void * __cdecl Foo::qt_metacast(char const *)" (?qt_metacast#Foo##UEAAPEAXPEBD#Z)
LNK2001: unresolved external symbol "public: virtual int __cdecl Foo::qt_metacall(enum QMetaObject::Call,int,void * *)" qt_metacall#Foo##UEAAHW4Call#QMetaObject##HPEAPEAX#Z)
LNK1120: 3 unresolved externals
I have obviously tried run qmake, clean and rebuild without effect. If I make the class Foo a plain C++ class by removing all Qt references, it works correctly.
What could be wrong?

"unresolved external symbol" error

Please have a look at the following code
Main.cpp
#include <iostream>
#include <string>
using namespace std;
int main()
{
system("pause");
return 0;
}
Magic.h
#pragma once
class Magic
{
public:
Magic();
~Magic();
virtual void display()=0;
};
Spell.h
#pragma once
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
class Spell :
public Magic
{
public:
Spell(void);
Spell(string words);
~Spell(void);
void display();
private:
string words;
};
Spell.cpp
#include "Spell.h"
#include "Magic.h"
#include <iostream>
#include <string>
using namespace std;
Spell::Spell(void)
{
}
Spell::Spell(string words)
{
this->words = words;
}
Spell::~Spell(void)
{
cout << "Delete Spell" << endl;
}
void Spell::display()
{
cout << "Spell Words: " << words << endl;
}
Here, I am getting the error
1>------ Build started: Project: Revision1_1, Configuration: Debug Win32 ------
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::~Magic(void)" (??1Magic##QAE#XZ) referenced in function __unwindfunclet$??0Spell##QAE#XZ$0
1>Spell.obj : error LNK2019: unresolved external symbol "public: __thiscall Magic::Magic(void)" (??0Magic##QAE#XZ) referenced in function "public: __thiscall Spell::Spell(void)" (??0Spell##QAE#XZ)
1>C:\Users\yohan\Documents\Visual Studio 2010\Projects\Revision1_1\Debug\Revision1_1.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do not understand what to do here. Why is this happening? Please help! I am new to C++ anyway..
Magic is not implementing its constructor and destructor (which also should be virtual).
Don't even declare the constructor if not necessary, e.g.
class Magic {
public:
virtual ~Magic() {}
virtual void display() = 0;
};
Unrelated: I didn't know you can display magic.
You didn't implement
Magic();
~Magic();
You'll need to either implement them inline, in an implementation file, or mark them = default.
You have declared a destructor in your Magic class but did not define it. That's why the linker complains (and the compiler doesn't).
You don't have an implementation for Magic. If your intention is for Magic to be an abstract base class, then just change its declaration to:
#pragma once
class Magic
{
public:
virtual void display()=0;
};
Remember, any method that is not followed by = 0 in the interface must be implemented in the class.

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.

Unresolved externals [Constructors] [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I have a problem with the Linker which I just can't solve..
Already tried anything I could think of
I have a Baseclass (Person) and a Derived Class (Dealer) and I just want to call the Constructor from the CardStack Class which is a member in the Dealer class.
Here is my code:
Person.h
#ifndef PERSON_H
#define PERSON_H
#include "Card.h"
#include "Hand.h"
class Person
{
public:
Person(void);
virtual ~Person(void);
virtual bool TakeCard(Card c);
virtual bool Lost(void);
protected:
virtual void CheckLost(void);
bool b_Lost;
Hand m_Hand;
};
#endif
Dealer.h
#ifndef DEALER_H
#define DEALER_H
#include "Person.h"
#include "Card.h"
#include "CardStack.h"
class Dealer : public Person
{
public:
Dealer(int stackcount);
virtual ~Dealer(void);
bool TakeCard(Card c);
bool Lost(void);
Card GiveCard(Card c);
protected:
void CheckLost(void);
CardStack m_GameStack;
};
#endif
Dealer.cpp
#include "Dealer.h"
Dealer::Dealer(int stackcount) : Person(), m_GameStack(stackcount)
{
};
Dealer::~Dealer(void)
{
};
bool Dealer::TakeCard(Card c)
{
if(!b_Lost || m_Hand.GetTotal() <= 17)
{
m_Hand.Take(c);
CheckLost();
return true;
}
return false;
};
void Dealer::CheckLost()
{
if (m_Hand.GetTotal() > 21)
{
b_Lost = true;
}
};
bool Dealer::Lost()
{
return b_Lost;
};
I honestly tried everthing I could think of but I couldn't figure out what the mistake is...
Here is the Output when compiling Dealer.cpp:
1>Dealer.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall Person::~Person(void)" (??1Person##UAE#XZ) referenced in function __unwindfunclet$??0Dealer##QAE#H#Z$0
1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::TakeCard(class Card)" (?TakeCard#Person##UAE_NVCard###Z)
1>Dealer.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall Person::Lost(void)" (?Lost#Person##UAE_NXZ)
1>Dealer.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall Person::CheckLost(void)" (?CheckLost#Person##MAEXXZ)
It looks like you are trying to compile Dealer.cpp into a program on its own. That doesn't work because it depends on the definition of the methods in Person, which are probably in Person.cpp. It would have been helpful if you had shown us the command you used to compile. But assuming you're using g++, what you probably tried to do is
g++ Dealer.cpp
What you should have done is either
g++ Person.cpp Dealer.cpp etc.
or
g++ -c Dealer.cpp
g++ -c Person.cpp
etc., and then
g++ Dealer.o Person.o etc.

LNK2019 linking error in C++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++ template, linking error
I have two linking errors and I have no idea what's wrong with the code and how to fix them:
main.obj:-1: error: LNK2019: unresolved external symbol "public:
__thiscall A::A(void)" (??0?$A#VB####QAE#XZ) referenced in function "public: __thiscall B::B(void)" (??0B##QAE#XZ)
and
main.obj:-1: error: LNK2019: unresolved external symbol "public: void
__thiscall A::exec(void (__thiscall B::*)(void))" (?exec#?$A#VB####QAEXP8B##AEXXZ#Z) referenced in function "public:
void __thiscall B::run(void)" (?run#B##QAEXXZ)
Explaining the code a little:
This class has to execute a function from the derived class. function exec is called from the derived class with a function from the derived class parameter. Signature of this function is void function();
//header.h
#ifndef HEADER_H
#define HEADER_H
template <class T>
class A
{
public:
typedef void (T::*ExtFunc)();
A();
void funcA();
void exec(ExtFunc func);
};
#endif // HEADER_H
//header.cpp
#include "header.h"
template<typename T>
A<T>::A() { }
template<typename T>
void A<T>::funcA()
{
cout << "testA\n";
}
template<typename T>
void A<T>::exec(ExtFunc func)
{
(T().*func)();
}
In main.cpp I derive a class from A class and pass the derived class as template paramtere. Then I execute function exec through the run() function.
//main.cpp
#include <iostream>
#include "header.h"
using namespace std;
class B : public A<B>
{
public:
B() { }
void run()
{
exec(&B::funcB);
}
void funcB()
{
cout << "testB\n";
}
};
int main()
{
B ob;
ob.run();
return 0;
}
Can anyone tell me what's going on?...
When you are using templates, generally you cannot put the implementation in a .cpp file - you have to put the whole class in the header. So move all the code from header.cpp to the .h.
You can get around this by doing an explicit instantiation inside the .cpp file - instantiating the template for a particular type. But this requires that you know ahead of time which types will need an instantiation and will prevent you from adding new instantiations. The only benefit is a reduction in compile time.