class 未在范围内声明,但 class 已声明

class not declared in scope but the class is declared

我不知道如何描述这个,但我声明了一个 class (movableBox) 但我不能在特定范围内使用(这个范围在 class 播放器中)。

这是我的一些代码:

玩家class:

#ifndef PLAYER_H
#define PLAYER_H

#include <SFML/Graphics.hpp>
#include <vector>
#include "movablebox.h"

class player
{
    public:
        sf::RectangleShape* player;
        sf::Texture* playerTexture;
        sf::Vector2f speed;
        bool touching;

        static void create(sf::Vector2f pos_);
        static void draw(sf::RenderWindow& window);
        static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids, std::vector<movableBox::movableBox_*> movableBoxes);
        static void checkControls();
        static void checkControlsperEvent (sf::Event& event);
};

#endif // PLAYER_H

和 movableBox class:

#ifndef MOVABLEBOX_H
#define MOVABLEBOX_H

#include <SFML/Graphics.hpp>
#include <vector>
#include "player.h"

class player;

class movableBox
{
    public:

        struct movableBox_ {
            sf::RectangleShape* box;
            sf::Texture* texture;
            sf::Vector2f speed;
            bool selected;
            player* selectedBy;
            bool touching;
        };

        static void create(sf::Vector2f pos_, sf::Vector2f size_);
        static void draw(sf::RenderWindow& window);
        static void updatePos(float& dt, float gravity, std::vector<sf::RectangleShape*> solids);
        static void grabNearest(player* player);
};

#endif // MOVABLEBOX_H

我收到此错误:

CodeBlocksProjects/phys/player.h:18:110: error: ‘movableBox’ was not declared in this scope

正如我缺乏解释所表明的那样,我不知道为什么会发生这种情况,也不知道它是如何发生的,希望您能理解我的问题。 提前致谢! :)

这是一个循环依赖问题。 player.h中有#include "movablebox.h"movablebox.h中也有#include "player.h"

您在 movablebox.h 中转发了 class player 似乎足够了;所以只需从 movablebox.h.

中删除 #include "player.h"