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)"
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
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;
}
//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.
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;
};
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();