我一直试图找出代码中未正确链接或声明的内容。我正在使用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();
}
}
}
这是很长的...错误消息
错误LNK2019无法解析的外部符号“ public:__thiscall Zx :: IStateMachine :: IStateMachine(class std :: list,class std :: allocator>,struct std :: hash,class std :: allocator>>,struct std: :equal_to,类std :: allocator>>,类std :: allocator,类std :: allocator>>>,类std :: allocator,类std :: allocator>,结构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>>> * *)“(?? 0IStateMachine @ Zx @@ QAE @ PAPAV?$ list @ V?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U?$ hash @ V? $ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ U?$ equal_to @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V $ allocator @ _W @ 2 @@ std @@@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @ @std @@@ 2 @@ std @@ V?$ allocator @ V?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U $ hash @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ U?$ equal_to @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @ @std @@@@ 2 @@ std @@@ 2 @@ std @@ PAPAV?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @ @U?$ hash @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@@ 2 @ U?$ equal_to @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @函数“ public:__thiscall TestStateMachine :: TestStateMachine(class std :: list,class std :: allocator>,struct std :: hash,class std ::''中引用的2 @@ std @@@@ 2 @@ 3 @@ Z))分配器>>,结构std :: equal_to,类std :: allocator>>,类std :: allocator,类std :: allocator>>>,类std :: allocator,类std :: allocator>,结构std: :hash,class std :: allocator>>,struct std :: equal_to,class std :: allocator>>,class std :: allocator,class std :: allocator>>>>> * *,class std :: unordered_set,类std :: allocator>,结构std :: hash,类std :: allocator>>,结构std :: equal_to,类std :: allocator>>,类std :: allocator,类std :: allocator>>> * *)“(?? 0TestStateMachine @@ QAE @ PAPAV?$ list @ V?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U $ hash @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ U?$ equal_to @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @ @std @@@ 2 @@ std @@ V?$ allocator @ V?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U $ hash @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ U?$ equal_to @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ alloca tor @ _W @ 2 @@ std @@@ 2 @@ std @@@ 2 @@ std @@ PAPAV?$ unordered_set @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@ U?$ hash @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ U?$ equal_to @ V? $ basic_string @ _WU?$ char_traits @ _W @ std @@ V?$ allocator @ _W @ 2 @@ std @@@ 2 @ V?$ allocator @ V?$ basic_string @ _WU?$ char_traits @ _W @ std @@ V ?$ allocator @ _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);
}
};
答案 0 :(得分:0)
假设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();
}
}
答案 1 :(得分:0)
我删除了所有宏内容,并删除了缺少头文件的#include
,提供了tstring
之类的typedef std::basic_string<TCHAR> tstring;
的替代品,以及const tstring& IState::name() { return pm_name; }
的实现,并且代码得以编译没有错误。