循环依赖:无法删除不完整的类型



我不明白为什么我会收到这个编译器错误:

error C2027: use of undefined type 'GameState'
note: see declaration of 'GameState'
error C2338: can't delete an incomplete type warning C4150: deletion of pointer to incomplete type 'GameState'; no destructor called

这是相关代码:

#pragma once
#include <SFMLGraphics.hpp>
#include "SpawnManager.h"
#include "Resource.h"
#include <stack>
#include <memory>
class GameState;
class Controller
{
public:
    Controller();
    void run();
    void setPlayerScore(unsigned score);
    sf::RenderWindow& getWindow() { return m_window; }
    void addState(const States& state);
    void changeState(const States& state);
    GameState* getState() const;
    void popState();
    void add_state(const States& type, Controller * cont);
    ~Controller() {}
private:
    SpawnManager<States, GameState> m_sFactory;
    sf::RenderWindow m_window;
    ScoreRecord m_playerScore;
    std::stack<std::unique_ptr<GameState>> m_screens;
};
#pragma once
#include <SFMLGraphics.hpp>
#include <memory>
#include "Controller.h"
//State design pattern
class GameState
{
public:
    explicit GameState(Controller* state_holder);
    GameState(const GameState&) = delete;
    GameState(GameState&&) = delete;
    GameState& operator=(const GameState&) = delete;
    GameState& operator=(GameState&&) = delete;
    virtual ~GameState() = default;
    virtual void displayState() = 0;
    virtual void updateStage() = 0;
    virtual void handleEvent(sf::Event& event) = 0;
protected:
    std::unique_ptr<Controller> m_state;
};

有什么想法可以解决这个问题吗?

std::unique_ptr<T>析构函数的定义要求T是完整的,即定义,而不仅仅是声明。由于std::unique_ptr<GameState>Controller的间接成员,析构函数~Controller的定义需要定义std::unique_ptr<GameState>的析构函数,因此也需要GameState的定义,而的定义没有提供。

解决方案:在定义~Controller之前定义GameState。一个最小的例子:

struct GameState;
// 1. Definition of Controller
struct Controller
{
    ~Controller();
    std::stack<std::unique_ptr<GameState>> m_screens;
};
// 2. Definition of GameState
struct GameState
{
    std::unique_ptr<Controller> m_state;
};
// 3. Definition of Controller::~Controller
Controller::~Controller(){} // or = default;

附言:考虑到你永远不可能有一个GameState被指向同一GameStateController所指向。在这种情况下,您最终会无限递归破坏已经破坏的对象。多个GameStates不能拥有相同的Controller,反之亦然。考虑您的所有权结构是否有意义。我怀疑您需要共享所有权或非所有者推荐。

相关内容

  • 没有找到相关文章

最新更新