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;
}
Related
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
in a MSVC dll project, i tried to create a dll containing a base class
//header file
class MATHLIBRARY_API Shape {
protected:
int width, height;
public:
Shape(int, int);
int area(void) { return -1; };
};
it's compiled successfully, but when adding a virtual specifier to the function
class MATHLIBRARY_API Shape {
protected:
int width, height;
public:
Shape(int, int);
virtual int area(void) { return -1; };
};
the compiler showed error msg
Error LNK2019 unresolved external symbol `__declspec(dllimport) const Shape::`vftable'" (__imp_??_7Shape##6B#) referenced in function "public: __thiscall Shape::Shape(int,int)" (??0Shape##QAE#HH#Z) Dll3 c:\Users\langj\source\repos\Dll3\Dll3\Dll3.obj 1
where could be the problem?
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)"
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;
}
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;
};