[英]vs2013: static member does not link
我正在嘗試為我正在處理的一個項目編寫一個簡單的 FSM。 我從這個鏈接獲得了 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 ==========
使用 "OffState::theOffState" 的唯一點不是函數調用 - 它是對在類中聲明的單例的引用。
車輛::車輛 () : mState(&OffState::theOffState()) { }
我在試圖弄清楚它時頭疼得厲害,但仍然一無所獲。 任何的想法?
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
意味着您的代碼有聲明,但沒有theOffState
定義。 我認為您正在嘗試編譯的代碼示例尚未完成。
我猜設計模式概述的作者通過注釋行“// 單例設計模式”以及狀態模式示例中的“// 在 VehState 方法實現中實現默認行為”行暗示了一些東西
編輯:
我添加了一些代碼,只是為了使其可編譯(但您應該考慮邏輯並繼續編碼):
#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.