unresolved linking error in a virtual function - c++

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

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)"

Strange Unresolved External Symbol in C++/CLI

I have a LNK2001 error which I cannot solve!
My Solution is composed by a native project (DLL) and a managed one (DLL wrapper).
The native DLL works if is used with a native test application.
Now, the native code is the following (all defined into the namespace EuroSDK::Protocols) :
The EUROSDK_API is defined as __declspec(dllexport)
IMessageDispatcher.h
class EUROSDK_API IMessageDispatcher {
public:
IMessageDispatcher() : m_ProtocolAdapter(0) {};
virtual ~IMessageDispatcher() {};
virtual ERROR_CODE SendMessage(IMessage & msg) = 0;
virtual ERROR_CODE SendMessage(IMessage & msg, STRING destination) {
RAISE_AND_RETURN(EC_NOT_IMPLEMENTED);
};
virtual ERROR_CODE SendMessageSync(IMessage & msg) {
RAISE_AND_RETURN(EC_NOT_IMPLEMENTED);
};
virtual ERROR_CODE SendMessageSync(IMessage & msg, IMessage & ret_msg) {
RAISE_AND_RETURN(EC_NOT_IMPLEMENTED);
};
virtual ERROR_CODE SendMessageRaw(char* buff, UINT size) {
RAISE_AND_RETURN(EC_NOT_IMPLEMENTED);
};
ERROR_CODE SetProtocolAdapter(IProtocolAdapter * adapter) {
m_ProtocolAdapter = adapter;
return EC_NO_ERRORS;
}
protected:
IProtocolAdapter* m_ProtocolAdapter;
};
class EUROSDK_API INetMessageDispatcher : public IMessageDispatcher {
public:
virtual ERROR_CODE Init(STRING stationName,
STRING userName,
BOOL crypthData = FALSE) = 0;
virtual ERROR_CODE Connect(UINT portNumber,
STRING remoteIP,
UINT connectionTimeOut = 5000) = 0;
virtual ERROR_CODE Disconnect() = 0;
};
NetMessageDispatcher.h
class EUROSDK_API NetMessageDispatcher : public INetMessageDispatcher
{
...
ERROR_CODE SendMessage(IMessage & msg);
ERROR_CODE SendMessage(IMessage & msg, STRING destination);
...
};
NetMessageDispatcher.cpp
ERROR_CODE NetMessageDispatcher::SendMessage(IMessage & msg) {
... implementation ...
}
ERROR_CODE NetMessageDispatcher::SendMessage(IMessage & msg, STRING destination) {
... implementation ...
}
Now, into the managed wrapper I have:
NetMessageDispatcherNET.h
using namespace EurocSDK::Protocols;
namespace EuroSDKNET {
namespace Protocols {
class NetMessageDispatcherWrap;
public ref class NetMessageDispatcherNET {
public:
NetMessageDispatcherNET(EDispatcherType stationType,
MessageEventHandlerNET^ handler)
~NetMessageDispatcherNET() {};
// Implemented in the cpp file
virtual int SendMessage(IMessageNET^ msg);
virtual int SendMessage(IMessageNET^ msg, String^ destination);
private:
CAutoNativePtr<NetMessageDispatcherWrap> m_Dispatcher;
};
// Unmanaged Wrapper
private class NetMessageDispatcherWrap : public ISessionFactory,
public NetMessageDispatcher {
public:
~NetMessageDispatcherWrap() {}
NetMessageDispatcherWrap(MessageEventHandlerNET^ handler,
NetMessageDispatcherNET^ dispatcher,
IMessageFactory::Ptr messageFactory,
EuroSDKNET::Protocols::EDispatcherType stationType) :
NetMessageDispatcher(this,
messageFactory,
Logger::get("NetMessageDispatcher"),
(EuroSDK::Protocols::EDispatcherType)stationType),
m_handler(handler),
m_Dispatcher(dispatcher) {}
/// Creates an instance of a Session,
/// using the given message handler.
inline Session* createSession(const IMessageDispatcher* dispatcher) {
..... implementation ....
}
private:
gcroot<MessageEventHandlerNET^> m_handler;
gcroot<NetMessageDispatcherNET^> m_Dispatcher;
};
}
}
NetMessageDispatcherNET.cpp
NetMessageDispatcherNET::NetMessageDispatcherNET(EuroSDKNET::Protocols::EDispatcherType stationType,
MessageEventHandlerNET^ handler) {
m_Dispatcher = new NetMessageDispatcherWrap(handler,
this,
new EuroSDK::Protocols::PBMessageFactory(),
stationType);
}
NetMessageDispatcherNET::~NetMessageDispatcherNET() {}
int NetMessageDispatcherNET::SendMessage(IMessageNET^ msg) {
return 0;
}
int NetMessageDispatcherNET::SendMessage(IMessageNET^ msg, String^ destination) {
return 0;
}
Now, when I build the NET DLL I get:
1>NetMessageDispatcher.obj : warning LNK4248: unresolved typeref token (01000017) for 'boost.detail.win32._SECURITY_ATTRIBUTES'; image may not run
1>NetMessageDispatcherWrapper.obj : error LNK2001: unresolved external symbol "public: virtual enum E_ErrorCode __thiscall EuroSDK::Protocols::NetMessageDispatcher::SendMessage(class EuroSDK::Protocols::IMessage &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?SendMessage#NetMessageDispatcher#Protocols#EuroSDK##UAE?AW4E_ErrorCode##AAVIMessage#23#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
1>NetMessageDispatcherWrapper.obj : error LNK2001: unresolved external symbol "public: virtual enum E_ErrorCode __thiscall EuroSDK::Protocols::NetMessageDispatcher::SendMessage(class EuroSDK::Protocols::IMessage &)" (?SendMessage#NetMessageDispatcher#Protocols#EuroSDK##UAE?AW4E_ErrorCode##AAVIMessage#23##Z)
What could cause the problem?
Daniele.
Based on your new comments and your edit
in your IMessageDispatcher class you are defining
virtual ERROR_CODE SendMessage(IMessage & msg) = 0;
virtual ERROR_CODE SendMessage(IMessage & msg, STRING destination) {
RAISE_AND_RETURN(EC_NOT_IMPLEMENTED);
};
Now INetMessageDispatcher inherits those 2 functions from IMessageDispatcher which means they can be called on an INetMessageDispatcher instance after youi implement the pure virtual one.
Next, NetMessageDispatcher inherits those two functions from INetMessageDispatcher and that is a redefinition, so your solution is
1) either remove the redifinitions from the NetMessageDispatcher class, they are unnecessary
2) If you use C++ 11 you could use an override keyword
class EUROSDK_API NetMessageDispatcher : public INetMessageDispatcher
{
...
virtual ERROR_CODE SendMessage(IMessage & msg) override;
}

error LNK2019 unresolved external symbol virtual class

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