C++ C++;:循环依赖问题

C++ C++;:循环依赖问题,c++,circular-dependency,C++,Circular Dependency,我有一个循环依赖的问题,我想这是一个设计缺陷,以错误的方式引入游戏类 游戏h: #pragma once #include <SFML\Graphics.hpp> #include "GameScreen.h" #include "TitleScreen.h" class Game { protected: sf::RenderWindow window; GameScreen* CurrentGameScreen; TitleScreen Title;

我有一个循环依赖的问题,我想这是一个设计缺陷,以错误的方式引入游戏类

游戏h:

#pragma once
#include <SFML\Graphics.hpp>

#include "GameScreen.h"
#include "TitleScreen.h"

class Game
{
protected:
    sf::RenderWindow window;

    GameScreen* CurrentGameScreen;
    TitleScreen Title;

public:
    Game(void);
    ~Game(void);

    sf::RenderWindow getWindow(void);

    void Run();
    void Close();
};
标题Screen.h:

#pragma once

#include "Game.h"

class GameScreen
{
public:
    GameScreen(void);
    ~GameScreen(void);

    virtual void LoadAllResources() {};
    virtual void DrawScreen(Game* game) {};
    virtual void Update(Game* game) {};
};
#pragma once
#include <SFML\Graphics.hpp>

#include "GameScreen.h"

class TitleScreen : public virtual GameScreen
{
private:
    sf::Texture title_screen;
    sf::Sprite titleScreen;

    sf::Font font;
    sf::Text menuExit;

public:
    TitleScreen(void);
    ~TitleScreen(void);

    void LoadAllResources();
    void DrawScreen(Game* game);
    void Update(Game* game);
};
GameScreen.h和TitleScreen.h提高了一把C2061。据我所知,这是由Game.h和Gamescreen.h之间的循环依赖性造成的

TitleScreen.h给了我错误C2504:“游戏屏幕”:基类未定义

h:在第12行和第13行,给出C2143:语法错误:缺少“;”在“*”之前,虽然我不确定这是从哪里来的,而且我的IDE没有给我任何语法错误

如果我从GameScreen.h中删除
#include
语句,并用向前声明
类游戏替换它(我想这会打破循环依赖?)上面的大部分问题都解决了,但是TitleScreen.cpp会在我每次尝试访问游戏对象时抛出一组C2027C2227C2228未定义类型左的->左的。)。IntelliSense指出,不允许使用指向不完整类的指针


在引入游戏类之前,我已经让它工作了-
DrawScreen()
Update()
将使用指向窗口的指针(
sf::RenderWindow*window
)作为参数。main.cpp.

中还有一部分旧代码留在
GameScreen.h
中,您应该声明
Game
类,而不是包含其整个头文件,因此:

class Game;
而不是:

#include "Game.h"

GameScreen.h
中,您应该声明
Game
类,而不是包括其整个头文件,因此:

class Game;
而不是:

#include "Game.h"

回答为@piokuc,你需要一个回答为@piokuc,你需要一个