无法从存储在向量中的对象访问属性

时间:2016-07-07 22:34:32

标签: c++ pointers vector state

嘿嘿我在这里是一个c ++ newb ...我会指责某人指出我对指针和向量的明显误解(可能是别的东西)。我试图创建一个处理多个子状态的开关元素

class MotionSwitch: public StateSwitch, public IMotion
{
public:
  MotionSwitch(){ initialize(); };
  ~MotionSwitch(){};

  //Make Motion States
  MotionState restState; //Initialized In _states at 0
  MotionState motionState;//"                     " 1
  MotionState spinState;//"                       " 2

  std::vector<MotionState*>  _states;

};

class MotionState: public State, public IMotion
{
...
String msg = "Hello Is Motion";
...
};

_states向量初始化如下...

void MotionSwitch::initialize()
{
  //Add States In Order Of ENUM (Because It Matters!)
  _states.push_back(&restState);
  _states.push_back(&motionState);
  _states.push_back(&spinState);
}

以下应用程序代码中只会打印以下其中一项

  MotionState *currentState = widget._motionSwitch._states.at(0);
  widget._com.log( "State Message...");
  widget._com.log( currentState -> msg); //Won't Print
  widget._com.log( widget._motionSwitch.restState.msg ); //This WILL Print

我很感激任何帮助,我可以从你的代码神来到这里!

0 个答案:

没有答案