我在为班级实施状态机时遇到了麻烦。我不断收到错误:
state.cpp:5: error: have0 was not declared in this scope
state.cpp:10: error: redefinition of State* Have0State::process(std::string)
state.h:18: error: virtual State* Have0State::process(std::string) previously defined here
在继续使用机器的其余部分之前,我试图让 Have0State 工作,因此代码稀疏。
状态.h:
#ifndef STATE_H
#define STATE_H
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <iostream>
class State{
public:
State(){};
virtual State* process(std::string input) = 0;
};
class Have0State: public State {
public:
Have0State():State(){};
virtual State* process(std::string input);
}have0;
#endif
状态.cpp:
#include "state.h"
using namespace std;
State *currentState = &have0;
State* Have0State::process(string input){
if(input == "quarter"){
cout << "cool" << endl;
}
return &have0;
}
int main(int argc, char** argv) {
string input;
//get input
cin >> input;
while (input != "exit") {
currentState = currentState->process(input);
//get input
cin >> input;
}
return 0;
};
我尝试将流程功能定义为,Have0State::State::process(string input)
但这也不起作用。关于函数指针应该如何工作的任何说明,特别是在子类成员函数的上下文中,我将不胜感激。
编辑:另外,state.h 文件中类声明have0
末尾的声明到底是什么?Have0State
它没有明确声明的类型;是否暗示它是 Have0State 类型?