error LNK2019 unresolved external symbol virtual class - c++

I know this question was asked several time, but i don't find how to resolve it.
I get this error when i'm trying to build my project:
error LNK2019: unresolved external symbol "public: virtual __thiscall IGameState::~IGameState(void)" (??1IGameState##UAE#XZ) in function "public: virtual __thiscall MenuState::~MenuState(void)" (??1MenuState##UAE#XZ)
Here is my code:
IGameState.h
class IGameState
{
public:
virtual ~IGameState();
virtual void update() = 0;
virtual void render() = 0;
};
MenuState.h
#include "IGameState.h"
class MenuState : public IGameState
{
public:
MenuState();
~MenuState();
void update();
void render();
};
MenuState.cpp
#include "MenuState.h"
#pragma region Constructor
MenuState::MenuState() {
}
MenuState::~MenuState() {
}
#pragma endregion
void MenuState::render() {
}
void MenuState::update() {
}
What's wrong with the destructor?
Thanks.

The error message tells you that's a link error, because you haven't implemented ~IGameState(),
Try add below code:
class IGameState
{
public:
virtual ~IGameState() {}
//^^^^ define it
virtual void update() = 0;
virtual void render() = 0;
};

Related

Unresolved External Symbol how to fix it? abstract class

Here's my abstract class Storestate.h:
#ifndef STORESTATE_H_
#define STORESTATE_H_
class Store;
class StoreState
{
public:
virtual void Close(Store&);
virtual void Open(Store&);
virtual void Sell(Store&);
};
#endif
The Derived class header file ConcreteStateOpened.h:
#ifndef CONCRETESTATEOPENED_H_
#define CONCRETESTATEOPENED_H_
#include "StoreState.h"
#include "Store.h"
class ConcreteStateOpened : public StoreState
{
public:
ConcreteStateOpened() {}
void Open(Store&) override;
void Close(Store&) override;
void Sell(Store&) override;
};
#endif
The Dervied class cpp file ConcreteStateOpened.cpp:
#include "ConcreteStateOpened.h"
#include <iostream>
using namespace std;
void ConcreteStateOpened::Open(Store &store)
{
cout << store.Name << " is already opened!" << endl;
}
void ConcreteStateOpened::Close(Store &store)
{
store.State = ConcreteStateOpened();
}
void ConcreteStateOpened::Sell(Store &store)
{
std::cout << "Sell Opened";
}
I don't know how to fix this. I tried removing override keywords, aswell as virtual ones. Even removing the definition etc. I just need help from pros :,)
Here are the unresolved external symbol errors:
1>ConcreteStateOpened.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Close(class Store &)" (?Close#StoreState##UAEXAAVStore###Z)
1>Data.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Close(class Store &)" (?Close#StoreState##UAEXAAVStore###Z)
1>StateStore.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Close(class Store &)" (?Close#StoreState##UAEXAAVStore###Z)
1>ConcreteStateOpened.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Open(class Store &)" (?Open#StoreState##UAEXAAVStore###Z)
1>Data.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Open(class Store &)" (?Open#StoreState##UAEXAAVStore###Z)
1>StateStore.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Open(class Store &)" (?Open#StoreState##UAEXAAVStore###Z)
1>ConcreteStateOpened.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Sell(class Store &)" (?Sell#StoreState##UAEXAAVStore###Z)
1>Data.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Sell(class Store &)" (?Sell#StoreState##UAEXAAVStore###Z)
1>StateStore.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall StoreState::Sell(class Store &)" (?Sell#StoreState##UAEXAAVStore###Z)
1>D:\Local-Git-Repos\DesignPatterns\StateC++\StateStore\Debug\StateStore.exe : fatal error LNK1120: 3 unresolved externals
You didn't make the methods in the abstract base pure virtual.
Also you should add : public virtual destructor, protected default constructor and remove copy/move/assignment constructors
#ifndef STORESTATE_H_
#define STORESTATE_H_
class Store;
class StoreState
{
public:
virtual ~StoreState() = default;
StoreState(const StoreState&) = delete;
StoreState(StoreState&&) = delete;
StoreState& operator=(const StoreState&) = delete;
virtual void Close(Store&) = 0;
virtual void Open(Store&) = 0;
virtual void Sell(Store&) = 0;
protected:
StoreState() = default; // prevent accidental creation
};
#endif
Your StoreState isn't actually abstract. You have declared Close, Open, Sell as actual virtual functions with definitions. To declare a virtual function with no definition in that class, use the "pure virtual" syntax = 0.
Also, it's best practice for a polymorphic class type to have a virtual destructor. And per the Rule Of Five, when you declare a destructor, be sure to think about the copy/move constructor and copy/move assignment. For an interface class, it's often best to just make it uncopyable and unassignable.
class StoreState
{
public:
StoreState() = default;
StoreState(const StoreState&) = delete;
StoreState& operator=(const StoreState&) = delete;
virtual ~StoreState() = default;
virtual void Close(Store&) = 0;
virtual void Open(Store&) = 0;
virtual void Sell(Store&) = 0;
};
For starters there is no abstract class in the presented code by you because the class StoreState does not contain pure virtual functions.
From the C++ 20 Standard (11.7.3 Abstract classes)
2 A virtual function is specified as a pure virtual function by using
a pure-specifier (11.4) in the function declaration in the class
definition. [Note: Such a function might be inherited: see below. —
end note] A class is an abstract class if it has at least one pure
virtual function. [Note: An abstract class can be used only as a
base class of some other class; no objects of an abstract class can be
created except as subobjects of a class derived from it (6.2, 11.4). —
end note] A pure virtual function need be defined only if called with,
or as if with (11.4.6), the qualified-id syntax
You need either to provide definitions of the virtual functions declared in the class StoreState or to make them pure virtual functions like for example
class StoreState
{
public:
virtual void Close(Store&) = 0;
virtual void Open(Store&) = 0;
virtual void Sell(Store&) = 0;
};
Even if a function is declared as a pure virtual function nevertheless you may provide its definition (outside the class definition where it is declared as a pure virtual function) if it is required.
Pay attention to that when you have polymorphic classes then you should declare the destructor also as virtual. For example
class StoreState
{
public:
virtual ~StoreState() = default;
virtual void Close(Store&) = 0;
virtual void Open(Store&) = 0;
virtual void Sell(Store&) = 0;
};
You may need to add virtual to the concrete class methods
#ifndef CONCRETESTATEOPENED_H_
#define CONCRETESTATEOPENED_H_
#include "StoreState.h"
#include "Store.h"
class ConcreteStateOpened : public StoreState
{
public:
ConcreteStateOpened() {}
virtual void Open(Store&) override;
virtual void Close(Store&) override;
virtual void Sell(Store&) override;
};
#endif

LNK2019 Error when calling methods of a static member in .cpp file

I am getting these errors in my code, they are all related to one static member in my Game class.
Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class MainMenuScene> __thiscall SceneManager::AddScene<class MainMenuScene>(void)" (??$AddScene#VMainMenuScene###SceneManager##QAE?AV?$shared_ptr#VMainMenuScene###std##XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init#Game##QAEXPBDHH_N#Z)
Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class GameSelectScene> __thiscall SceneManager::AddScene<class GameSelectScene>(void)" (??$AddScene#VGameSelectScene###SceneManager##QAE?AV?$shared_ptr#VGameSelectScene###std##XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init#Game##QAEXPBDHH_N#Z)
Game.obj : error LNK2019: unresolved external symbol "public: class std::shared_ptr<class MainMenuScene> __thiscall SceneManager::ChangeScene<class MainMenuScene>(void)" (??$ChangeScene#VMainMenuScene###SceneManager##QAE?AV?$shared_ptr#VMainMenuScene###std##XZ) referenced in function "public: void __thiscall Game::init(char const *,int,int,bool)" (?init#Game##QAEXPBDHH_N#Z)
In my game.h I have:
#pragma once
class SceneManager;
using namespace std;
class Game {
public:
Game();
~Game();
void init(const char* title, int width, int height, bool fullscreen);
void handleEvents();
void update();
void render();
void clean();
bool isRunning() { return running; }
static SDL_Renderer * renderer;
static SceneManager * sceneManager;
};
The errors are happening in my game.cpp file in the init() method and my game.cpp looks like:
#include "Game.h"
#include "SceneManager.h"
#include "GameSelectScene.h"
#include "MainMenuScene.h"
SceneManager * Game::sceneManager = new SceneManager();
Game::Game() {}
Game::~Game() {}
void Game::init(const char* title, int width, int height, bool fullscreen) {
// I do some stuff up here
// Here is where I think errors are happening.
sceneManager->AddScene<MainMenuScene>();
sceneManager->AddScene<GameSelectScene>();
sceneManager->ChangeScene<MainMenuScene>();
}
My GameSelectScene.h and MainMenuScene.h are both subclasses of my Scene.h
and my SceneManager is where the AddScene and ChangeScene methods are defined
My SceneManager.h:
#pragma once
#include "Game.h"
#include "Scene.h"
#include <map>
#include <string>
#include <typeindex>
using namespace std;
class SceneManager {
public:
SceneManager();
~SceneManager();
void init();
void update();
void render();
template <typename T> std::shared_ptr<T> ChangeScene();
template <typename T> std::shared_ptr<T> AddScene();
private:
std::map<type_index, std::shared_ptr<Scene>> scenes;
std::shared_ptr<Scene> currentScene;
};
SceneManager.cpp:
#include "SceneManager.h"
SceneManager::SceneManager() {}
SceneManager::~SceneManager() {}
// I define the init() update() and render()
template <typename T> std::shared_ptr<T> SceneManager::ChangeScene() {
type_index index(typeid(T));
currentScene = scenes[index];
return static_pointer_cast<T>(scenes[index]);
}
template <typename T> std::shared_ptr<T> SceneManager::AddScene() {
T scene = new T();
scenes[std::type_index(typeid(*scene))] = scene;
return scene;
}
If I remove my AddScene and ChangeScene method calls in the game.cpp file everything compiles correctly and the update, init and render methods defined in the SceneManager run perfectly. I have been researching this issue and walked through all the problems described in MSDN LNK2019 Error
Template functions must be defined in header files, not cpp.
Why can templates only be implemented in the header file?

Sometimes there is a link error under VS2012, sometimes not

Today I've got a strange link error under VS 2012. When I tried to build my solution after some coding I got link error (LNK2001). When I canceled changes just by commenting added lines, the project builds. With some tries I got a strange thing. (next lines describes my actions in actual order)
When I build project with all lines, I'm getting LNK2001.
When I comment first constructor for GeneticNetworkRealisation - I'm getting LNK2001.
When I comment second constructor for GeneticNetwork - error disappears.
But when I return this line back to code - there is still no error.
So, this means that two identical code has different link success. Any ideas of how to fix this? Simplified code and errors:
NeuralNetwork.h
#pragma once
#include "Neuron.h"
#include "NeuralLayer.h"
struct Topology {
// some variables
};
class NeuralNetwork {
public:
NeuralNetwork(const Topology & topology);
NeuralNetwork(const NeuralNetwork & nn) : layers(nn.layers) {}
virtual ~NeuralNetwork() {}
protected:
std::vector<NeuralLayer> layers;
};
GeneticNetwork.h
#pragma once
#include "NeuralNetwork.h"
class GeneticNetwork : public NeuralNetwork {
public:
GeneticNetwork(const Topology & topology) : NeuralNetwork(topology) {}
GeneticNetwork(const GeneticNetwork & gnn) : NeuralNetwork(gnn) {}
virtual std::vector<double> getNetworkGeneticCode() const = 0;
virtual void mutate() = 0;
virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const = 0;
virtual ~GeneticNetwork() {}
};
GeneticNetworkRealisation.h
#pragma once
#include "GeneticNetwork.h"
#include "URNG.h"
struct MutationParameters {
// some parameters
};
const MutationParameters DEFAULT_MUTATION_PARAMS = {/*...*/};
class GeneticNetworkRealisation : public GeneticNetwork {
public:
GeneticNetworkRealisation(const GeneticNetworkRealisation & gnn) :
GeneticNetwork(gnn), random(gnn.random), mutationParams(gnn.mutationParams) {}
GeneticNetworkRealisation(const Topology & topology, URNG urng = URNG(),
const MutationParameters & mutationParameters = DEFAULT_MUTATION_PARAMS) :
GeneticNetwork(topology), random(urng), mutationParams(mutationParameters) {}
virtual std::vector<double> getNetworkGeneticCode() const override;
virtual void mutate() override;
virtual NeuralNetwork crossingOver(const NeuralNetwork & nn) const override;
virtual ~GeneticNetworkRealisation() {}
protected:
URNG & random;
MutationParameters mutationParams;
};
GeneticNetworkRealisation.cpp
#include "GeneticNetworkRealisation.h"
void GeneticNetworkRealisation::mutate() {
// code
}
std::vector<double> GeneticNetworkRealisation::getNetworkGeneticCode() const {
// code
}
NeuralNetwork GeneticNetworkRealisation::crossingOver(const NeuralNetwork & nn) const {
// code
}
Errors:
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class std::vector<double,class std::allocator<double> > __thiscall GeneticNetworkRealisation::getNetworkGeneticCode(void)const " (?getNetworkGeneticCode#GeneticNetworkRealisation##UBE?AV?$vector#NV?$allocator#N#std###std##XZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual void __thiscall GeneticNetworkRealisation::mutate(void)" (?mutate#GeneticNetworkRealisation##UAEXXZ)"
1>main.obj : error LNK2001: unresolved external symbol ""public: virtual class NeuralNetwork __thiscall GeneticNetworkRealisation::crossingOver(class NeuralNetwork const &)const " (?crossingOver#GeneticNetworkRealisation##UBE?AVNeuralNetwork##ABV2##Z)"

unresolved linking error in a virtual function

I'm getting an unresolved linking error for a couple of virtual functions.
I have an interface IFieldManager and I implement it using ImplFieldManager.
So I have two functions that needs to be implemented FieldToArea() and FieldToPoi(). which are already implemented.
The full error message:
Severity Code Description Project File Line Suppression State
Error LNK2001 unresolved external symbol "public: virtual bool __thiscall ImplFieldManager::FieldToArea(struct S3W::IField const &,struct S3W::IArea &)" (?FieldToArea#ImplFieldManager##UAE_NABUIField#S3W##AAUIArea#3##Z) Schiebel3DWorld (Libraries\Schiebel3DWorld\Schiebel3DWorld) D:\andre\OSGEarthLib_VS2015-build\Schiebel3DWorld\src\ImplIConnection.obj 1
My code :
#include "Schiebel3DWorld/include/IArea.h"
#include "Schiebel3DWorld/include/IField.h"
#include "Schiebel3DWorld/include/IPoi.h"
namespace S3W
{
struct IFieldManager
{
IFieldManager() {}
virtual ~IFieldManager() {}
virtual bool FieldToArea(const IField& areas, IArea& manager) = 0;
virtual bool FieldToPoi(const IField& areas, IPoi& manager) = 0;
};
}
namespace S3W
{
struct IFieldManager;
struct IServerConnection : public CImplRefPtr, virtual public IRefPtr
{
virtual IFieldManager* GetFieldManager() = 0;
};
}
namespace S3W
{
struct ImplConnection : public S3W::IServerConnection, public SNet::SThread
{
// Inherited via IServerConnection
virtual S3W::IFieldManager * GetFieldManager() override;
};
}
#include "Schiebel3DWorld/include/IFieldManager.h"
struct ImplFieldManager : public S3W::IFieldManager
{
ImplFieldManager(){}
~ImplFieldManager() {}
// Inherited via IFieldManager
virtual bool FieldToArea(const S3W::IField & areas, S3W::IArea & manager) override;
virtual bool FieldToPoi(const S3W::IField & areas, S3W::IPoi & manager) override;
};
S3W::IFieldManager * S3W::ImplConnection::GetFieldManager()
{
return new ImplFieldManager();
}
#include "ImplFieldManager.h"
bool ImplFieldManager::FieldToArea(const S3W::IField & areas, S3W::IArea & manager)
{
return false;
}
bool ImplFieldManager::FieldToPoi(const S3W::IField & areas, S3W::IPoi & manager)
{
return false;
}

Template Specialization with External Errors

When I attempted to specialize one of my template functions, Visual Studio threw me an external error, including an error for a function that was not specialized.
The three errors:
1>------ Build started: Project: Project3, Configuration: Debug Win32 ------
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::reverse(void)" (?reverse#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::print(void)" (?print#?$linearList#VFriendToken####UAEXXZ)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall linearList<class FriendToken>::insertionSort(void)" (?insertionSort#?$linearList#VFriendToken####UAEXXZ)
Here is the relevant part of the code:
template<class T>
class arrayList : public linearList<T>
{
public:
//other methods
void reverse();
void print();
void insertionSort();
};
template<class T>
void arrayList<T>::reverse()
{
//method body
}
template<>
void arrayList<FriendToken>::insertionSort()
{
//method body
}
template<>
void arrayList<FriendToken>::print()
{
//method body
}
template<class T>
void arrayList<T>::insertionSort(){}
template<class T>
void arrayList<T>::print(){}
Your example shows specializations of the arrayList member functions which I assume are supposed to be overriding their virtual equivalants in linearList. The linker is saying it cant find the virtual members in the class linearList which is not included in your example.
virtual void __thiscall linearList<class FriendToken>::reverse(void)
If I add a definition of linearList like this the linker is quiet (on MSVC2010, I also added a empty FriendToken class to make things work).
template<typename T>
class linearList
{
public:
virtual void reverse() = 0; //pure virtual
virtual void print() = 0;
virtual void insertionSort() = 0;
};
If this is not your problem please post the code for linearList and I will update my answer as that is surely the source of your problem.
If needed for reference here is how I used the function reverse to test:
arrayList<FriendToken> a;
static_cast<linearList<FriendToken>&>(a).reverse();