链接器错误 - 不确定它没有链接
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了链接器错误 - 不确定它没有链接相关的知识,希望对你有一定的参考价值。
我一直试图弄清楚我的代码中没有链接或声明的内容。我正在用C ++编写一个简单的游戏引擎用于学习目的,我正在尝试创建一个状态机。链接器错误与unordered_list,list或map有关。我不确定哪个。错误是有两个参数的构造函数,它是两个typedef并包含在我显示的代码中,当我调用该构造函数时会发生错误。
IState.h
#ifndef ZX_STATE_H
#define ZX_STATE_H
#include "coreDefines.h"
#include "zxstring.h"
ZX_NS_START
class IState
{
tstring pm_name;
public:
const tstring& name();
virtual ~IState() = default;
virtual void Pre() = 0;
virtual void Run() = 0;
virtual void Post() = 0;
};
ZX_NS_END
#endif
IStateMachine.h
#ifndef ZX_ISTATEMACHINE_H
#define ZX_ISTATEMACHINE_H
#include "object.h"
#include "zxstring.h"
#include "IState.h"
#include <unordered_set>
#include <map>
#include <list>
ZX_NS_START
typedef std::map<tstring, IState*> StateMap;
typedef std::unordered_set<tstring> StateSet;
typedef std::list<StateSet> RunOrder;
class IStateMachine : object, IState
{
public:
const tstring defaultState = T("zx default state");
IStateMachine();
IStateMachine(RunOrder** runOrder, StateSet** states);
virtual ~IStateMachine();
void Add(IState* state);
void Remove(IState* state);
private:
StateMap pm_states;
RunOrder** pm_runOrder;
StateSet** pm_activeStates;
void Pre() override;
void Run() override;
void Post() override;
void SetUpDefaultState();
class DefaultState: IState
{
IStateMachine* pm_parent;
public:
DefaultState();
explicit DefaultState(IStateMachine* parent);
void Pre() override;
void Run() override;
void Post() override;
};
};
ZX_NS_END
#endif
IStateMachine.cpp
#include "IStateMachine.h"
USINGZX;
IStateMachine::IStateMachine(): pm_states(StateMap()), pm_runOrder(nullptr), pm_activeStates(nullptr)
{
SetUpDefaultState();
}
IStateMachine::IStateMachine(RunOrder** runOrder, StateSet** states) : pm_states(StateMap()), pm_runOrder(runOrder), pm_activeStates(states)
{
SetUpDefaultState();
}
IStateMachine::~IStateMachine()
{
delete pm_states[defaultState];
}
void IStateMachine::Add(IState* state)
{
pm_states.insert(std::make_pair(state->name(), state));
}
void IStateMachine::Remove(IState* state)
{
pm_states.erase(state->name());
}
void IStateMachine::Pre()
{
pm_states[defaultState]->Pre();
}
void IStateMachine::Run()
{
pm_states[defaultState]->Run();
}
void IStateMachine::Post()
{
pm_states[defaultState]->Post();
}
void IStateMachine::SetUpDefaultState()
{
DefaultState* def = new DefaultState(this);
pm_states.insert(std::make_pair(defaultState, (IState*)def));
}
IStateMachine::DefaultState::DefaultState() :pm_parent(nullptr)
{
}
IStateMachine::DefaultState::DefaultState(IStateMachine* parent): pm_parent(parent)
{
}
void IStateMachine::DefaultState::Pre()
{
for(const auto& states : **pm_parent->pm_runOrder)
{
for(const auto& runState : states)
{
pm_parent->pm_states[runState]->Pre();
}
}
}
void IStateMachine::DefaultState::Run()
{
for(const auto& states: **pm_parent->pm_runOrder)
{
for(const auto& runState : states)
{
pm_parent->pm_states[runState]->Run();
}
}
}
void IStateMachine::DefaultState::Post()
{
for (const auto& states : **pm_parent->pm_runOrder)
{
for (const auto& runState : states)
{
pm_parent->pm_states[runState]->Post();
}
}
}
这是long ... looong错误消息
错误LNK2019未解析的外部符号“public:__ thiscall Zx :: IStateMachine :: IStateMachine(类std :: list,类std :: allocator>,struct std :: hash,类std :: allocator >>,struct std :: equal_to, class std :: allocator >>,class std :: allocator,class std :: allocator >>>,class std :: allocator,class std :: allocator>,struct std :: hash,class std :: allocator >>, struct std :: equal_to,class std :: allocator >>,class std :: allocator,class std :: allocator >>>>> * *,class std :: unordered_set,class std :: allocator>,struct std :: hash,class std :: allocator >>,struct std :: equal_to,class std :: allocator >>,class std :: allocator,class std :: allocator >>> * *)“(?? 0IStateMachine @ Zx @@ QAE @ PAPAV?$ @名单V'$ @ unordered_set V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@性病@@ U&$ @哈希V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$分配器@ _W @ 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @ @@ 2 @@性病@@ V'$ @分配器V'$ @ unordered_set V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@性病@@ U&$哈希@V?$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @ @@ 2 @@ STD @@@ 2 @@性病@@ PAPAV?$ @ unordered_set V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@性病@@ú? $ @哈希V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string _WU?$ @ char_traits _W @性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ std @@@ 2 @@ 3 @@ Z)在函数“public:__thiscall TestStateMachine :: TestStateMachine(class std :: list,class std :: allocator>,struct std :: hash,class std :: allocator >>>中引用,struct std :: equal_to,class std :: allocator >>,class std :: allocator,class std :: allocator >>>,class std :: allocator,class std :: allocator>,struct std :: hash ,类std :: allocator >>,struct std :: equal_to,class std :: allocator >>,class std :: allocator,class std :: allocator >>>>> * *,class std :: unordered_set,class std :: allocator>,struct std :: hash,class std :: allocator >>,struct std :: equal_to,class std :: allocator >>,class std :: allocator,class std :: allocator >>> * *) “(?? 0TestStateMachine @@ QAE @ PAPAV?$ list @ V?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U?$哈希@ V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string _WU?$ @ char_traits @ _W STD @@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @@性病@@ V'$ @分配器V'$ @ unordered_set V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@性病@@ U&$哈希@ V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$ @的basic_string _WU?$ @ char_traits @ _W STD @@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$分配器@ _瓦@ 2 @@ STD @@@ 2 @@ STD @@@ 2 @@性病@@ PAPAV?$ @ unordered_set V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@性病@@ U&$ @哈希V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ U&$ @ equal_to V'$的basic_string @_WU?$ @ char_traits @ _W性病@@ V'$ @分配器@ _W 2 @@ STD @@@ 2 @ V'$ @分配器V'$ @的basic_string _WU?$ @ char_traits @ _W性病@@ V'$分配器@ _W @ @@ 2 STD @@@ 2 @@ 2 @@ Z)
我通常可以很容易地解决这些问题,但是出于什么原因这个问题让我感到困惑。(可能与错误信息中的不可读类型有关)
我不相信这会产生任何后果,但是,这里是调用链接器错误的测试代码
StateMachineTest.cpp
class TestStateMachine : zx IStateMachine
{
public:
TestStateMachine(zx RunOrder** order, zx StateSet** active) : zx IStateMachine(order, active)
{
}
};
TEST_CLASS(StateMachineTest)
{
public:
TEST_METHOD(IStateMachineTest)
{
auto* order = new zx RunOrder();
auto* active = new zx StateSet();
TestStateMachine test = TestStateMachine(&order, &active);
}
};
我删除了你所有的宏东西,#include
s缺少头文件,提供了tstring
的typedef std::basic_string<TCHAR> tstring;
的替代品和const tstring& IState::name() { return pm_name; }
的实现,并且代码编译没有错误。
假设USINGZX
是using namespace zx;
,那么这个代码没有在IStateMachine::IStateMachine
命名空间中定义zx
,它在全局命名空间中。
USINGZX;
IStateMachine::IStateMachine(): pm_states(StateMap()), pm_runOrder(nullptr), pm_activeStates(nullptr)
{
SetUpDefaultState();
}
如果你想在IStateMachine::IStateMachine
命名空间中定义zx
那么你要么说
zx::IStateMachine::IStateMachine(): pm_states(StateMap()), pm_runOrder(nullptr), pm_activeStates(nullptr)
{
SetUpDefaultState();
}
要么
namespace zx {
IStateMachine::IStateMachine(): pm_states(StateMap()), pm_runOrder(nullptr), pm_activeStates(nullptr)
{
SetUpDefaultState();
}
}
以上是关于链接器错误 - 不确定它没有链接的主要内容,如果未能解决你的问题,请参考以下文章