[英]vs2013: static member does not link
I'm trying to write a simple FSM for one project that I'm working on.我正在尝试为我正在处理的一个项目编写一个简单的 FSM。 I got the GoF State sample from this link but it seems to be already bugged - when I try to build it into vs2013 I got the following error:
我从这个链接获得了 GoF State 示例,但它似乎已经被窃听了 - 当我尝试将它构建到 vs2013 时,我收到以下错误:
1>------ Build started: Project: Poco-stateMachine, Configuration: Release Win32 ------
1>Server.obj : error LNK2001: unresolved external symbol "public: static class OffState & __cdecl OffState::theOffState(void)" (?theOffState@OffState@@SAAAV1@XZ)
1>C:\Users\Woody\Documents\Visual Studio 2013\Projects\Poco-tcpServer4\Release\Poco-stateMachine.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
The only point where the "OffState::theOffState" is used is not a function call - its a ref to one singleton declared inside a class.使用 "OffState::theOffState" 的唯一点不是函数调用 - 它是对在类中声明的单例的引用。
Vehicle::Vehicle () : mState(&OffState::theOffState()) { }
车辆::车辆 () : mState(&OffState::theOffState()) { }
I got one huge headache trying to figure it out and still nothing.我在试图弄清楚它时头疼得厉害,但仍然一无所获。 Any idea?
任何的想法?
The FSM Sample Code: FSM 示例代码:
> class Vehicle {
> public:
> Vehicle ();
> // whatever other ctors are needed
> void turnOn(); // { mState->turnOn(*this); }
> void engageGear(); // { mState->engageGear (*this); }
> void disengageGear(); //{ mState->disengageGear (*this); }
> void turnOff(); //{ mState->turnOff (*this); }
> // other operations
> private:
> friend class VehState;
> void changeState (VehState* newState); // { mState = newState; }
> private:
> VehState* mState;
> };
>
> class VehState {
> public:
> virtual void turnOn(Vehicle&); // allows changing Vehicle object state pointer
> virtual void engageGear(Vehicle&); // same as above
>
> virtual void disengageGear(Vehicle&);
> virtual void turnOff(Vehicle&);
> protected:
> void changeState (Vehicle& veh, VehState* newState) { veh.changeState(newState); }
> };
>
> class MovingState : public VehState {
> public:
> static MovingState& theMovingState(); // Singleton design pattern
> virtual void disengageGear(Vehicle& veh);
> };
>
> class IdleState : public VehState {
> public:
> static IdleState& theIdleState(); // Singleton design pattern
> virtual void engageGear(Vehicle& veh) {changeState(veh, &MovingState::theMovingState()); }
> };
>
> class OffState : public VehState {
> public:
> static OffState& theOffState(); // Singleton design pattern
> virtual void turnOn(Vehicle& veh) { changeState(veh, &IdleState::theIdleState()); }
> };
>
> // implement default behavior in VehState method implementations
> // implementations of Vehicle methods:
> Vehicle::Vehicle () :
> mState(&OffState::theOffState()) {}
>
> void Vehicle::turnOn() { mState->turnOn(*this); }
> void Vehicle::engageGear() { mState->engageGear (*this); }
> void Vehicle::disengageGear() { mState->disengageGear (*this); }
> void Vehicle::turnOff() { mState->turnOff (*this); }
>
> void Vehicle::changeState (VehState* newState) {
> mState = newState;
> }
>
unresolved symbol
means that your code has declaration, but doesn't have definition for theOffState
. unresolved symbol
意味着您的代码有声明,但没有theOffState
定义。 I think code example, you are trying to conpile, is not completed.我认为您正在尝试编译的代码示例尚未完成。
I guess the author of Design Patterns Overview hints at something by comment line “// Singleton design pattern”, as well as by line "// implement default behavior in VehState method implementations" in the example for State pattern我猜设计模式概述的作者通过注释行“// 单例设计模式”以及状态模式示例中的“// 在 VehState 方法实现中实现默认行为”行暗示了一些东西
EDIT:编辑:
I have added some code, just to make it compilable (but you should think over logic and continue coding):我添加了一些代码,只是为了使其可编译(但您应该考虑逻辑并继续编码):
#include <string>
#include <iostream>
using namespace std;
class Vehicle {
public:
Vehicle ();
// whatever other ctors are needed
void turnOn(); // { mState->turnOn(*this); }
void engageGear(); // { mState->engageGear (*this); }
void disengageGear(); //{ mState->disengageGear (*this); }
void turnOff(); //{ mState->turnOff (*this); }
// other operations
private:
friend class VehState;
void changeState (VehState* newState); // { mState = newState; }
private:
VehState* mState;
};
class VehState {
public:
virtual void turnOn(Vehicle&); // allows changing Vehicle object state pointer
virtual void engageGear(Vehicle&); // same as above
virtual void disengageGear(Vehicle&);
virtual void turnOff(Vehicle&);
protected:
void changeState (Vehicle& veh, VehState* newState) { veh.changeState(newState); }
};
class MovingState : public VehState {
public:
static MovingState& theMovingState() // Singleton design pattern
{
static MovingState stateMoving;
return stateMoving;
}
virtual void disengageGear(Vehicle& veh) {}
};
class IdleState : public VehState {
public:
static IdleState& theIdleState() // Singleton design pattern
{
static IdleState stateIdle;
return stateIdle;
}
virtual void engageGear(Vehicle& veh) {changeState(veh, &MovingState::theMovingState()); }
};
class OffState : public VehState {
public:
static OffState& theOffState() // Singleton design pattern
{
static OffState stateOff;
return stateOff;
}
virtual void turnOn(Vehicle& veh) { changeState(veh, &IdleState::theIdleState()); }
};
// implement default behavior in VehState method implementations
void VehState::turnOn(Vehicle& obj) {
obj.turnOn();
}
void VehState::engageGear(Vehicle& obj) {
obj.engageGear();
}
void VehState::disengageGear(Vehicle& obj) {
obj.disengageGear();
}
void VehState::turnOff(Vehicle& obj) {
obj.turnOff();
}
// implementations of Vehicle methods:
Vehicle::Vehicle () :
mState(&OffState::theOffState()) { }
void Vehicle::turnOn() {
mState->turnOn(*this);
}
void Vehicle::engageGear() {
mState->engageGear (*this);
}
void Vehicle::disengageGear() {
mState->disengageGear (*this);
}
void Vehicle::turnOff() {
mState->turnOff (*this);
}
void Vehicle::changeState (VehState* newState) {
mState = newState;
}
int main()
{
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.