用 C++ 编写状态机
Coding State Machine in C++
我正在尝试编写基于口香糖机的状态机。我有一个基本状态的接口class,而我有使用这个接口的特定状态。我有四种状态,no_quarter
、has_quarter
、sold
和 sold_out
状态。我还有一个口香糖机 class 可以处理这些状态,并且根据我的机器所处的状态,它会 class 并执行所需的操作。这是我的代码有问题,我也会 post 我的函数。
Gumball_Machine.h
class Gumball_Machine
{
private:
int gumball_count;
State *current_state;
No_Quarter_State *nqs;
Has_Quarter_State *hqs;
Sold_State *ss;
Sold_Out_State *sos;
public:
Gumball_Machine(int inventory)
{
gumball_count = inventory;
nqs = new No_Quarter_State(this);
hqs = new Has_Quarter_State(this);
ss = new Sold_State(this);
sos = new Sold_Out_State(this);
if (gumball_count == 0)
set_state(sos);
else
set_state(nqs);
}
void insert_quarter()
{
current_state->insert_quarter();
}
void eject_quarter()
{
current_state->eject_quarter();
}
void turn_crank()
{
current_state->turn_crank();
}
void dispense()
{
current_state->dispense();
}
void set_state(State *new_state)
{
current_state = new_state;
}
State *get_no_quarter_state()
{
return nqs;
}
State *get_has_quarter_state()
{
return hqs;
}
State *get_sold_state()
{
return ss;
}
State *get_sold_out_state()
{
return sos;
}
No_Quarter_State.h
#ifndef NO_QUARTER_STATE_H_INCLUDED
#define NO_QUARTER_STATE_H_INCLUDED
#include "State.h"
class No_Quarter_State: public State
{
public:
No_Quarter_State(Gumball_Machine *gbm);
void insert_quarter();
void eject_quarter();
void turn_crank();
void dispense();
};
#endif // NO_QUARTER_STATE_H_INCLUDED
No_Quarter_State.cpp
#include "No_Quarter_State.h"
#include "Gumball_Machine.h"
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
void No_Quarter_State::insert_quarter()
{
cout << "You inserted a quarter.\n";
machine->set_state(machine->get_has_quarter_state());
}
void No_Quarter_State::eject_quarter()
{
cout << "You must insert a quarter before you can eject one.\n";
}
void No_Quarter_State::turn_crank()
{
cout << "You must insert a quarter before you can crank the handle.\n";
}
void No_Quarter_State::dispense()
{
cout << "You need to pay first before you can get a gumball.\n";
}
我遇到问题的行在 No_Quarter_State.cpp
machine->set_state(machine->get_has_quarter_state());
这给我一个 运行 时间错误。我见过这样的例子,但我不完全确定这在 C++ 中是否合法。我正在尝试切换口香糖机对象的状态。
我收到的错误是一般性无响应错误:"test.ext has stopped working"
。我正在使用 CodeBlocks 对此进行编码。
在构造函数中,假设成员变量machine
被参数隐藏
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
您可以改用 initializer list syntax 来解决此问题:感谢 Sneftel 和 NathanOliver
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
: machine(machine)
{
}
但是,在常规方法函数中,如果您将方法参数命名为与成员变量相同的名称,则必须使用 this->
。用于避免该问题的典型样式是在成员名称前添加 m_
或附加 _
。
我正在尝试编写基于口香糖机的状态机。我有一个基本状态的接口class,而我有使用这个接口的特定状态。我有四种状态,no_quarter
、has_quarter
、sold
和 sold_out
状态。我还有一个口香糖机 class 可以处理这些状态,并且根据我的机器所处的状态,它会 class 并执行所需的操作。这是我的代码有问题,我也会 post 我的函数。
Gumball_Machine.h
class Gumball_Machine
{
private:
int gumball_count;
State *current_state;
No_Quarter_State *nqs;
Has_Quarter_State *hqs;
Sold_State *ss;
Sold_Out_State *sos;
public:
Gumball_Machine(int inventory)
{
gumball_count = inventory;
nqs = new No_Quarter_State(this);
hqs = new Has_Quarter_State(this);
ss = new Sold_State(this);
sos = new Sold_Out_State(this);
if (gumball_count == 0)
set_state(sos);
else
set_state(nqs);
}
void insert_quarter()
{
current_state->insert_quarter();
}
void eject_quarter()
{
current_state->eject_quarter();
}
void turn_crank()
{
current_state->turn_crank();
}
void dispense()
{
current_state->dispense();
}
void set_state(State *new_state)
{
current_state = new_state;
}
State *get_no_quarter_state()
{
return nqs;
}
State *get_has_quarter_state()
{
return hqs;
}
State *get_sold_state()
{
return ss;
}
State *get_sold_out_state()
{
return sos;
}
No_Quarter_State.h
#ifndef NO_QUARTER_STATE_H_INCLUDED
#define NO_QUARTER_STATE_H_INCLUDED
#include "State.h"
class No_Quarter_State: public State
{
public:
No_Quarter_State(Gumball_Machine *gbm);
void insert_quarter();
void eject_quarter();
void turn_crank();
void dispense();
};
#endif // NO_QUARTER_STATE_H_INCLUDED
No_Quarter_State.cpp
#include "No_Quarter_State.h"
#include "Gumball_Machine.h"
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
void No_Quarter_State::insert_quarter()
{
cout << "You inserted a quarter.\n";
machine->set_state(machine->get_has_quarter_state());
}
void No_Quarter_State::eject_quarter()
{
cout << "You must insert a quarter before you can eject one.\n";
}
void No_Quarter_State::turn_crank()
{
cout << "You must insert a quarter before you can crank the handle.\n";
}
void No_Quarter_State::dispense()
{
cout << "You need to pay first before you can get a gumball.\n";
}
我遇到问题的行在 No_Quarter_State.cpp
machine->set_state(machine->get_has_quarter_state());
这给我一个 运行 时间错误。我见过这样的例子,但我不完全确定这在 C++ 中是否合法。我正在尝试切换口香糖机对象的状态。
我收到的错误是一般性无响应错误:"test.ext has stopped working"
。我正在使用 CodeBlocks 对此进行编码。
在构造函数中,假设成员变量machine
被参数隐藏
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
{
machine = machine;
}
您可以改用 initializer list syntax 来解决此问题:感谢 Sneftel 和 NathanOliver
No_Quarter_State::No_Quarter_State(Gumball_Machine *machine)
: machine(machine)
{
}
但是,在常规方法函数中,如果您将方法参数命名为与成员变量相同的名称,则必须使用 this->
。用于避免该问题的典型样式是在成员名称前添加 m_
或附加 _
。