클래스 용 상태 시스템을 구현하는 데 문제가 있습니다. 나는 오류가 점점 계속 : 나는 기계의 나머지 부분, 따라서 스파 스 코드에 계속하기 전에 작업 할 수있는 Have0State을 얻으려고상태 기계, 하위 클래스 및 함수 포인터
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
.
state.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
state.cpp : 나는 Have0State::State::process(string input)
로 처리 함수를 정의하려고했지만 그 중 하나가 작동하지 않았다
#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;
};
. 함수 포인터가 어떻게 작동해야하는지에 대한 명확한 설명, 특히 서브 클래스 멤버 함수의 컨텍스트에서, 나는 그것을 매우 높이 평가할 것이다.
EDIT : 또한 정확히 have0
선언은 state.h 파일의 Have0State
클래스 선언 끝 부분에 있습니까? 명시 적으로 명시된 유형이 없습니다. 그것이 Have0State 유형이라는 것을 암시 하는가?
have0 정의는 구조 변수가 때때로 C에서 정의 된 것과 매우 비슷해 보입니다. 매우 드물고 나중에 문제가 발생할 수 있습니다 (헤더의 변수는 항상 그렇습니다). 그래서 다른 곳으로 옮겨야합니다. –
에는'state.h'의 정의 끝에'{}'이 있습니까? 그렇지 않으면'have0'을 리팩터링하여 도움이되는지 확인하십시오. –
오류를 생성 한 코드와 정확히 동일한 코드를 표시 하시겠습니까? 나는 헤더의 내용과 헤더 아래의 cpp 파일 내용을 온라인 컴파일러에'# ifndef' 가드를 생략하고 붙여 넣기 만하면된다. –