vs2013:静态成员没有链接

时间:2015-10-08 04:26:58

标签: c++ pointers visual-studio-2013 state

我正在尝试为我正在处理的一个项目编写一个简单的FSM。我从这个link获得了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”的唯一点不是函数调用 - 它是对类中声明的一个单例的引用。

  

Vehicle :: Vehicle():       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;
>   }
>

1 个答案:

答案 0 :(得分:2)

unresolved symbol表示您的代码已声明,但没有theOffState的定义。我认为代码示例,你试图调度,还没有完成。

我想Design Patterns Overview的作者通过注释行“// Singleton design pattern”提示某些内容,以及通过line" //在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;
}