状态模式 C++

8
我正在尝试引入一个简单的状态模式,之前在这里参考了一些优秀的教程:http://gameprogrammingpatterns.com/state.html 我已经完成了其中一半的教程,并且正试图通过将每个状态放进基类来复制静态实例。然而,在切换状态时,g++会抛出以下错误。
   state_test.cpp: In member function ‘virtual void Introduction::handleinput(Game&, int)’:
state_test.cpp:55:16: error: cannot convert ‘Playing*’ to ‘GameState*’ in assignment
    game.state_ = &GameState::play;
                ^

现在,我知道错误涉及指针的转换,但我真的很难看出如何修复它。当我跟随这个人的代码时,我有点期望它会起作用,但因为他在不断更改并试图加强最佳实践,所以我没有他完整的源代码可供跟随。然而,在我继续进行本教程的其余部分之前,我觉得理解代码是很重要的。

以下是我创建的代码,试图复制他的状态系统:

#include <iostream>

class Game;
class Introduction;
class Playing;

class GameState
{
public:

    static Introduction intro;
    static Playing play;

    virtual ~GameState() {std::cout << "an undefined GameState has been destroyed" << std::endl;}
    virtual void handleinput(Game& game, int arbitary) {}
    virtual void update(Game& game) {}

};

class Game
{
public:

    Game()
    {}
    ~Game()
    {}

    virtual void handleinput(int arbitary)
        {
            state_->handleinput(*this, arbitary);
        }

    virtual void update()
        {
            state_->update(*this);
        }

//private: 
    GameState* state_;
};

class Introduction : public GameState
{
public:

    Introduction()  
    {
        std::cout << "constructed Introduction state" << std::endl;
    }

    virtual void handleinput(Game& game, int arbitary) 
        {
            if (arbitary == 1)
            game.state_ = &GameState::play;
        }

    virtual void update(Game& game) {}
};

class Playing : public GameState
{
public:
    Playing()   {std::cout << "constructed Playing state" << std::endl;}

    virtual void handleinput(Game& game, int arbitary) 
        {
            if (arbitary == 0)
            game.state_ = &GameState::intro;
        }

    virtual void update(Game& game) {}
};

int main(int argc, char const *argv[])
{
    Game thisgame;

    return 0;
}

有什么想法,为什么我的实现无法编译?

编辑:

作为之前的辅导的回应,我非常感激,我修改了代码。我首先将它们放在不同的文件中,但是对于这样少量的测试代码来说,这更麻烦。我只是重写了一个声明类的头文件,然后在.cpp文件中定义了它们。

这是.h文件:

class Introduction;
class Playing;
class Game;
class GameState;

class GameState
{
    public:

    static Introduction intro;
    static Playing play;

    virtual ~GameState();
    virtual void handleinput(Game& game, int arbitary);
    virtual void update(Game& game);

};


class Introduction : public GameState
{
public:

    Introduction();

    virtual void handleinput(Game& game, int arbitary); 

    virtual void update(Game& game);

};

class Playing : public GameState
{
public:
    Playing();

    virtual void handleinput(Game& game, int arbitary);

    virtual void update(Game& game);    
};


class Game
{
public:

    Game();

    ~Game();

    virtual void handleinput(int arbitary);

    virtual void update();

    GameState* state_;

};

以下是 .cpp 文件:

#include <iostream>
#include "state.h"


GameState::~GameState() 
    {std::cout << "Exiting Game State Instance" << std::endl;}
void GameState::handleinput(Game& game, int arbitary) 
    {}
void GameState::update(Game& game) 
    {}



Game::Game()
    {}
Game::~Game()
    {}
void Game::handleinput(int arbitary)
        {
            state_->handleinput(*this, arbitary);
        }

void Game::update()
        {
            state_->update(*this);
        }


Introduction::Introduction()    
    {
        std::cout << "constructed Introduction state" << std::endl;
    }

void Introduction::handleinput(Game& game, int arbitary) 
        {
            if (arbitary == 1)
            game.state_ = &GameState::play;
        }

void Introduction::update(Game& game) {}


Playing::Playing()  
        {
            std::cout << "constructed Playing state" << std::endl;
        }

void Playing::handleinput(Game& game, int arbitary) 
        {
            if (arbitary == 0)
            game.state_ = &GameState::intro;
        }

void Playing::update(Game& game) {}



int main(int argc, char const *argv[])
{
    Game mygame;
    return 0;
}

我仍然无法让它工作。之前的错误已经消失了,但我仍然很难访问“introduction”的静态实例并在基础类中播放。抛出的错误是:

/tmp/ccH87ioX.o: In function `Introduction::handleinput(Game&, int)':
state_test.cpp:(.text+0x1a9): undefined reference to `GameState::play'
/tmp/ccH87ioX.o: In function `Playing::handleinput(Game&, int)':
state_test.cpp:(.text+0x23f): undefined reference to `GameState::intro'
collect2: error: ld returned 1 exit status

我本以为我已经搞清楚了!太沮丧了!

我应该补充一下,RustyX提供的答案确实可以编译,但是我必须将“playing”和“introduction”的实例移出类定义,这样我就不能将它们设置为静态的了。我认为这很重要,因为我只需要一个实例,并且希望尽早初始化它们。


你可能会对我的STTCL框架感兴趣。 - πάντα ῥεῖ
2个回答

7
问题在于编译器会从上到下读取文件。当遇到包含以下代码的行时:
game.state_ = &GameState::play;

他仍然不知道Playing继承自GameState。它只知道Playing是一个稍后会声明的类。
你应该将类的声明与方法的实现分开。先写所有类的声明,再写方法的实现。在更大的项目中,您可以将它们分成单独的*.h和*.cpp文件,这种排序会自然地发生。
简化例子:
class Playing : public GameState
{
public:
    Playing();

    virtual void handleinput(Game& game, int arbitary);

    virtual void update(Game& game);
};

// Declarations of other classes...


Playing::Playing() {
    std::cout << "constructed Playing state" << std::endl;
}

void Playing::handleinput(Game& game, int arbitrary) {
    if (arbitary == 0)
        game.state_ = &GameState::intro;
    }
}

void Playing::update(Game& game) {
}

您可以将一些方法留在类声明内部。通常,如果方法很小,受益于内联,并且没有这种循环依赖问题,则会执行此操作。


好的,这看起来很自信地回答了我的问题!谢谢! - William Barnes

3
将函数的实现移到所有类的定义之后。
编译器必须在完全看到继承类Playing和Introduction之后,才能知道它们是从GameState继承而来的。
#include <iostream>

class Game;
class Introduction;
class Playing;

class GameState
{
public:

    static Introduction intro;
    static Playing play;

    virtual ~GameState() {std::cout << "an undefined GameState has been destroyed" << std::endl;}
    virtual void handleinput(Game& game, int arbitary) {}
    virtual void update(Game& game) {}

};

class Game
{
public:

    Game()
    {}
    ~Game()
    {}

    virtual void handleinput(int arbitary)
        {
            state_->handleinput(*this, arbitary);
        }

    virtual void update()
        {
            state_->update(*this);
        }

//private: 
    GameState* state_;
};

class Introduction : public GameState
{
public:

    Introduction()  
    {
        std::cout << "constructed Introduction state" << std::endl;
    }

    virtual void handleinput(Game& game, int arbitary);

    virtual void update(Game& game) {}
};

class Playing : public GameState
{
public:
    Playing()   {std::cout << "constructed Playing state" << std::endl;}

    virtual void handleinput(Game& game, int arbitary);

    virtual void update(Game& game) {}
};

void Introduction::handleinput(Game& game, int arbitary) 
{
    if (arbitary == 1)
        game.state_ = &GameState::play;
}

void Playing::handleinput(Game& game, int arbitary) 
{
    if (arbitary == 0)
        game.state_ = &GameState::intro;
}

Introduction GameState::intro;
Playing GameState::play;


int main(int argc, char const *argv[])
{
    Game thisgame;

    return 0;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接