I'm trying to write a simple FSM for one project that I'm working on. 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:
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.
Vehicle::Vehicle () : mState(&OffState::theOffState()) { }
I got one huge headache trying to figure it out and still nothing. Any idea?
The FSM Sample Code:
> 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
. 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
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.