c++ - 用 C++ 编写状态机

标签 c++ codeblocks

我正在尝试编写基于口香糖机的状态机。我有一个基本状态的接口(interface)类,而我有使用此接口(interface)的特定状态。我有四种状态,no_quarterhas_quartersoldsold_out 状态。我还有一个处理这些状态的 gumball 机器类,根据我的机器所处的状态,它会进入该类并执行所需的操作。这是我的代码有问题,我也会发布我的函数。

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_ 或附加 _

关于c++ - 用 C++ 编写状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32256095/

相关文章:

c++ - 在 code::blocks 中配置 MSVC 调试器

c++ - 模板类中未使用的函数必须有效吗?

C++如何从类变量返回一个const对象

c++ - 如何在 C++ 中分配在 Python 中创建的对象?

windows - 在 linux 和 windows 中用 fortran 循环的最大元素

c++ - 代码块构建的 Exe 比 Visual Studio 构建的相同代码大将近 57 倍

SDL 2.0 : linking error

c++ - 如何使用 Code::Blocks 重建库?

c++ - 绘制圆弧并覆盖boundingRect()、shape()

java - 编程中的动态链接器是什么?