我正在尝试实现状态设计模式,但我不断得到基类未定义的错误。这些是我的文件。 的 MainClass.h
#pragma once
#include "State1.h"
#include "State.h"
class State;
class MainClass
{
public:
MainClass();
~MainCLass();
void changeState(AuctionState* newState);
private:
State *CurrentState;
};
在我的MainClass.cpp类中,changestate方法是
void MainClass::changeState(State * newState)
{
delete CurrentState;
CurrentState = newState;
}
State.h
#pragma once
#include "MainClass.h"
class MainClass;
class AuctionState
{
public:
virtual ~AuctionState();
virtual void run(MainClass& context);
};
State1.h
#pragma once
#include "State2.h"
#include "State.h"
class State2;
class MainState :
public AuctionState
{
public:
MainState();
~MainState();
};
State1.cpp
#include "State1.h"
MainState::MainState()
{
}
void run(MainClass& context)
{
//DO Stuff
context.changeState(new State2);
}
MainState::~MainState()
{
}
所有其他状态文件以相同的方式实现