【问题标题】:state pattern C++状态模式 C++
【发布时间】:2017-01-05 09:50:31
【问题描述】:

在学习了这里的一些优秀教程之后,我正在尝试创建一个简单的状态模式: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;
}

我仍然无法让它工作。以前的错误已经消失,但我正在努力访问“介绍”的静态实例并在基类中播放。抛出的错误是:

/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 提供的答案确实可以编译,但是我必须将“播放”和“介绍”的实例移到类定义之外,然后我不能再将它们设置为静态,我相信这很重要,因为我只需要每个实例一个,我希望它们尽早初始化。

【问题讨论】:

  • 你可能对我的STTCL 框架感兴趣。

标签: c++ class virtual state-pattern


【解决方案1】:

问题是编译器从上到下读取文件。在包含

的行
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) {
}

您可以在类声明中保留一些方法。如果方法很小,通常会这样做,会受益于内联并且没有这种循环依赖问题。

【讨论】:

  • 好的,这似乎可以自信地回答我的问题!谢谢!
【解决方案2】:

将函数的实现外线移动到所有类的定义之后。

编译器必须完全查看继承的类PlayingIntroduction,才能知道它们是从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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 2010-11-26
    • 2013-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多