Unresolved externals [Constructors] [duplicate] - c++

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.

Related

Unresolved external symbol c++ for inheritance and constructor

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

error LNK2019: unresolved external symbol classes

I get the error below when i call new TerrainClass() from the main, tried for hours to fix it, help please.
error LNK2019: unresolved external symbol "public: __thiscall TerrainClass::TerrainClass(void)" (??0TerrainClass##QAE#XZ) referenced in function "void __cdecl init(void)" (?init##YAXXZ)
GLDrawObject.h
#pragma once
class GLDrawObject
{
};
Terrain.cpp
#pragma once
TerrainClass::TerrainClass() : GLDrawObject()
{
}
Terrain.h
#pragma once
#include "GLDrawObject.h"
class TerrainClass : public GLDrawObject
{
public:
TerrainClass();
};
Firstly, your Terrain.cpp should be as follows:
#include "Terrain.h"
TerrainClass::TerrainClass() : GLDrawObject()
{
}
Secondly, you are getting a linker error, not a compiler error; once compiled, you need to link Terrain.o with the rest of your object files.

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

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;
}

error LNK2019: unresolved external symbol

#include <iostream>
using namespace std;
class A {
public:
void function( int num);
bool function1()const;
virtual bool function2() const=0;
};
class B:public A {
public :
bool function2()const;
};
int _tmain(int argc, _TCHAR* argv[])
{
void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针
// Edit: Google translation of the above comment is
// "Not address, but a pointer to a member function pointer"
bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数
// Edit: Google translation of the above comment is
// "Point to a member function pointer can point to a const member function"
B b;
A *a=&b;
(a->*p1)();
(b.*p1)();
return 0;
}
but when I link it:
1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1#A##QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function#A##QAEXH#Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2#B##UBE_NXZ)
can you tell me why?
You haven't implemented A::function(), A::function1(), or B::function2(). You need to do that.
A::function1, A::function and B::function2 are all declared, but never defined. You can't get a pointer to the function if it is not defined, where would it point?

Linker error LNK2019 while trying to compile prog with template declarations

Here the code
#include <iostream>
#include <conio.h>
using namespace std;
template <typename T> class grid
{
public:
grid();
~grid();
void createCells();
private:
T **cells;
};
int main(int argc, char **argv)
{
grid<int> intGrid;
_getch();
return 0;
}
While trying to compile - got a message:
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::~grid<int>(void)" (??1?$grid#H##QAE#XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall
grid<int>::grid<int>(void)" (??0?$grid#H##QAE#XZ) referenced in function _main
What need to do?
You need to define the constructor and destructor (you just declared them):
template <typename T> class grid
{
public:
grid()
{} // here
~grid()
{} // and here
void createCells();
private:
T **cells;
};