So I was trying to implement a chess game. I've got the most common link error:
error LNK2019: unresolved external symbol "public: __thiscall Bishop::~Bishop(void)" (??1Bishop##QAE#XZ) referenced in function _main
So here are the two related classes:
Bishop.h (There is no "Bishop.cpp")
#pragma once
#ifndef BISHOP_H
#define BISHOP_H
#include "ChessPiece.h"
class Bishop : public ChessPiece
{
public:
Bishop(bool isWhite) : ChessPiece(isWhite) {
}
~Bishop(void);
// pure virtual functions
virtual CellLocation *listAvailableMoves(void) {
return 0;
}
virtual char getPieceType() {
return PIECE_TYPE_BISHOP;
}
};
#endif
ChessPiece.h (there is no "ChessPiece.cpp")
#ifndef CHESSPIECE_H
#define CHESSPIECE_H
#include "Globals.h"
// Abstract class for inheritence
class ChessPiece {
public:
// Constructor
ChessPiece(bool isWhite) : m_isWhite(isWhite) {
}
~ChessPiece(void);
// pure virtual functions
virtual CellLocation *listAvailableMoves(void) = 0;
virtual char getPieceType() = 0;
// ACCESSORS, MUTATORS
// isWhite member
bool isWhite(void) const{
return m_isWhite;
}
void setIsWhite(bool isWhite) {
m_isWhite = isWhite;
}
protected:
bool m_isWhite;
};
#endif
In the "Globals.h" there are few definitions of these but it's unrelated to my function.
So I what I've done in main was simply:
Bishop somePiece(true);
cout << sizeof(somePiece) << endl;
But it gave out the LNK2019 error.
Now I know that the solution should be to adding default constructors to both classes (which didn't work for some reason) but I don't want them to be initialized with default values. Hence I don't want default constructors for any of these classes.
Is there any way that I do not create default constructors and get away with it?
You are missing the definitions of ~ChessPiece(void); and ~Bishop(void);. That's what the compiler is complaining about.
Also notice that when you declare ChessPiece(bool), the default ChessPiece() constructor is not available anymore: hence you won't be able to default construct a ChessPiece.
But if you are on C++11 and for some reasons you want to delete the default constructor manually, you can use:
ChessPiece() = delete;
Your problem is about the destructor:
unresolved external symbol "public: __thiscall Bishop::~Bishop(void)"
^
// Notice the ~
You declared a destructor but didn't provide an implementation for it.
Related
I am new to templates and I have searched the web for this error but I don't know how to fix it it.
Already checked Why can templates only be implemented in the header file?
State.h
template <class entityType>
class State
{
public:
State() = default;
virtual void Enter(entityType * owner);
};
EnterMine.h
#include "State.h"
class Miner;
class EnterMine : public State<Miner>
{
public:
EnterMine() = default;
virtual void Enter(Miner *) {
};
};
and Miner.cpp is blank
and the problem appears in main.cpp
#include "EnterMine.h"
int main()
{
EnterMine a;
}
The error I get is a linking error :
LNK2001 unresolved external symbol "public: virtual void __thiscall State::Enter(class Miner *)" (?Enter#?$State#VMiner####UAEXPAVMiner###Z)
(Note: this answer was written for the original question, it has been completely rewritten after that.)
Every function that is declared and used, should be defined somewhere.
It seems that you declare EnterMine::EnterMine() but never define it. If this constructor does nothing, either omit it (it will be implicitly defined by a compiler), or mark it as = default;.
class EnterMine : public State<Miner>
{
public:
EnterMine() = default;
...
};
This also applies to the State::State() constructor.
Even though it's a singleton, you're still calling the constructor. Thus, you will still need to define the constructor.
In fact, you need to define every function you declare in your header.
I've got code like this:
#pragma once
#include "MV/stateSystem/StateSystem.hpp"
#include "MV/config/Config.hpp"
namespace mv
{
class Initializator
{
/* ===Objects=== */
public:
protected:
private:
static Initializator *instance;
/* ===Methods=== */
public:
//Inits the program
void init();
static Initializator& getInstance();
static void createInstance();
protected:
private:
Initializator();
Initializator(Initializator const& copy) = delete; // Not Implemented
Initializator& operator=(Initializator const& copy) = delete; // Not Implemented
};
}
and .cpp
#include "Initializator.hpp"
namespace mv
{
Initializator* Initializator::instance;
void Initializator::init()
{
StateSystem::readStatesFromFile("data/states/states.txt");
}
Initializator & Initializator::getInstance()
{
if (instance == 0)
Logger::Log(constants::error::singleton::SINGLETON_NOT_INITED, Logger::STREAM::BOTH, Logger::TYPE::ERROR);
return *instance;
}
void Initializator::createInstance()
{
if (instance == 0)
instance = new Initializator();
}
}
but my compiler has got problem:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "private: __thiscall mv::Initializator::Initializator(void)" (??0Initializator#mv##AAE#XZ) referenced in function "public: static void __cdecl mv::Initializator::createInstance(void)" (?createInstance#Initializator#mv##SAXXZ)
I can't understand it because i've got other class where code is really similar and compiler hasn't got problem with it. In the past, when i've got this problem, i had to declare static members in .cpp file (for example: Initializator* Initializator::instance; ) but now it doesn't help me.
You are declaring the ctor, but not defining it
You can define it within class declaration with
Initializator() = default;
Or
Initializator(){};
Or also in the cpp with
Initializator::Initializator(){}
As the error message is saying, Initializator::createInstance() is calling the ctor, which has to be defined even if it is private
Declaring your default ctor as private prohibits the creation of an implicilty defined default ctor. Thus, you get the unresolved external symbol linker error.
To get it working, provide a valid ctor definition in your code.
Either in the .cpp file, or in the .hpp file ( by provding an in-class definition or writing ctor() = default forcing the automatic generation of a default constructor by the compiler ).
I am trying to achieve something similar to what is explained here.
I have an Interface class defined as:
class IInterface
{
public:
virtual bool foo() = 0;
virtual void destroy() = 0;
}
And and implementation class defined as:
class MyImplementation : public IInterface
{
MyImplementation();
~MyImplementation();
virtual bool foo();
virtual void destroy() {delete this;}
private:
MyImplementation(MyImplementation const&);
void operator=(MyImplementation const&);
}
extern "C" API MyInterface* __cdecl createMyImplementation();
This works fine in Release mode using VS2010, but in Debug mode, the compiler gives me this error:
MyImplementation.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall IInterface::IInterface(void)" (__imp_??0IInterface##QAE#XZ) referenced in function "public: __thiscall MyImplementation::MyImplementation(void)" (??0MyImplementation##QAE#XZ)
What is the problem and how can I fix this ?
From my understanding, we should not have virtual constructors... (see this question).
EDIT:
Fixed typo. foo does have a body, this is a simplified version of the real code.
Explanation of the why of the destroy function:
http://www.parashift.com/c++-faq-lite/delete-this.html
http://eli.thegreenplace.net/2011/09/16/exporting-c-classes-from-a-dll
virtual void destroy() {delete this};
What are you doing? Suiciding?
Be careful!
In your case, MyImplementation::foo() needs to have a body (i.e. defined), that's why you are getting linker error
It seems to be that your problem is that you aren't including the IInterface.obj file in the link. In release mode, the compiler realizes that the ctor is vacuous and removes calls to it. But it debug mode (for single-stepping through it), the ctor has to be there. I'm guessing that you just used the .h file.
I have files::
//ClassA.h
#ifndef ClassA_H
#define ClassA_H
#pragma once
class ClassA
{
public:
void func1(){
}
ClassA(void) {
}
~ClassA (void) {
}
};
#endif
//ClassA1.h
#include "ClassA.h"
class ClassA1 {
ClassA<2> b;
};
//ClassA1.cpp
#include "ClassA1.h"
//main_file.cpp
#include "ClassA1.h"
#include "iostream"
int main (int argc , char** argv) {
std::cout<<"in main"<<std::endl;
}
So this compiles fine...As soon as i define function of class ClassA outside the class in Class.h i get following error during build
1> LINK : c:\users\adudeja\documents\visual studio 2010\Projects\Test\Debug\Test.exe not found or not built by the last incremental link; performing full link
1>ClassA1.obj : error LNK2005: "public: void __thiscall ClassA::func1(void)" (?func1#ClassA##QAEXXZ) already defined in main_file.obj
1>ClassA1.obj : error LNK2005: "public: __thiscall ClassA::ClassA(void)" (??0ClassA##QAE#XZ) already defined in main_file.obj
1>ClassA1.obj : error LNK2005: "public: __thiscall ClassA::~ClassA(void)" (??1ClassA##QAE#XZ) already defined in main_file.obj
1>c:\users\adudeja\documents\visual studio 2010\Projects\Test\Debug\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
So what is the difference between defining function outside class and inside class.
Below is the non working code...
#ifndef ClassA_H
#define ClassA_H
#pragma once
class ClassA
{
public:
void func1();
ClassA(void);
~ClassA(void);
};
void ClassA::func1(){
}
ClassA::ClassA(void) {
}
ClassA::~ClassA (void) {
}
#endif
So what is the difference between defining function outside class and inside class.
When you define it in the class body it is implicitly inline, and an inline function can be defined in multiple files.
A non-inline function must be defined exactly once only.
So either put the non-inline definition into a single .cpp file, not in a header included by multiple files, or define it with the inline keyword.
OK, let's see...
In your example, no function is defined outside the class declaration.
Both ClassA1.cpp and main_file.cpp "see" the definition of ClassA. Since all functions defined inside a class declaration are considered inline, i.e. the linker does not see it as a seperate function at all, and has nothing to complain about.
But if you put the definition of e.g. func1() outside the class declaration (but still in ClassA), it's no longer considered inline, it's a seperate function. But as it is still visible to two compilation units, it gets compiled in both translation units.
So when you attempt to link the two object files together, you got two instances of func1(), and the linker complains.
The solution is to either:
define a function inside the class declaration, or
define the function in one seperate translation unit (ClassA.cpp), and link with that.
Using templates limits your options somewhat, as template code must be visible to each translation unit it is used in, i.e. you cannot just declare it in a header and implement it elsewhere, leaving you with only option 1. above.
Post-code-review:
ClassA.hpp:
#ifndef ClassA_HPP
#define ClassA_HPP
class ClassA
{
public:
ClassA();
~ClassA();
void func1();
};
#endif
ClassA.cpp:
#include "ClassA.hpp"
void ClassA::func1()
{
// ...
}
ClassA::ClassA()
{
// ...
}
ClassA::~ClassA()
{
// ...
}
ClassA1.hpp:
#ifndef ClassA1_HPP
#define ClassA1_HPP
#include "ClassA.hpp"
// Your example still assumed that ClassA is a template,
// so I twisted that into an inheritance instead since
// ClassA isn't a template anymore, and if it *were* a
// template, it would change things significantly.
// Perhaps trying too much at once?
class ClassA1 : public ClassA
{
// ...
};
#endif
ClassA1.cpp:
#include "ClassA1.hpp"
// ...
main_file.cpp:
#include "ClassA1.hpp"
#include <iostream>
int main( int argc, char * argv[] )
{
std::cout << "in main" << std::endl;
return 0;
}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I have problem with my simple data base program in C++ (with inheritance and virtual func.)
I've done class hirarchy which represents the object Weapon:
#ifndef Ammu_h
#define Ammu_h
#include <string>
#include <iostream>
using namespace std;
//////////HEADER FILE//////////////
class Weapon{
protected:
string name;
char damage;
public:
virtual void show() = 0;
//virtual ~Weapon();
};
class WhiteArm: public Weapon{
protected:
double sharpness;
double defence;
};
class Axe: public WhiteArm{
public:
Axe(string str, char dmg, double shrp, double def){
name = str;
damage = dmg;
sharpness = shrp;
defence = def;
};
void show(){
cout << this->name << this->damage << this->sharpness << this->defence << endl;
};
//class Sword: public WhiteArm{...};
//class Club: public WhiteArm{...};
};
#endif
First of all im not quite sure if my implementation is proper.
My main problem is that when I add a virtual destructor, I get error: LNK2001: unresolved external symbol "public: __thiscall Weapon::~Weapon(void)"
I thought it is necessary to make the destructor virtual when the base class contains virtual methods.
Is it good to make constructors at the end of hierarchy? (like me, upper)
I will appreciate every suggestion to my code
Thanks in advance
Your virtual destructor still needs to have an implementation, even if you mean for it to be pure virtual. I usually write them using this odd-looking syntax: virtual ~Weapon() = 0 {}.
But that evidently doesn't work with some compilers (all compilers other than Microsoft's?), and rightly so (C++11 draft § 10.4/2):
[ Note: A function declaration cannot provide both a pure-specifier
and a definition —end note ] [ Example:
struct C
{
virtual void f() = 0 { }; // ill-formed
};
Instead, you can either leave out the = 0 or locate the body outside the class definition.