类未在范围内声明但类已声明为

时间:2017-10-07 13:34:31

标签: c++ header-files

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

以下是我的一些代码:

玩家类:

#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类:

#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

正如你可以说我缺乏解释,我不知道为什么或如何发生这种情况,我希望你理解我的问题。 提前致谢! :)

1 个答案:

答案 0 :(得分:2)

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

你在player中有前瞻性的课程movablebox.h,似乎已经足够了;所以只需从#include "player.h"移除movablebox.h