错误C2079:' room :: goldC'使用未定义的类' goldContainer'

时间:2014-11-05 19:28:10

标签: visual-c++ c++11

我正在尝试创建一个有goldContainer的房间 goldContainer在单独的.h文件中定义。 当我试图编译它时说

  

错误C2079:'room :: goldC'使用未定义的类'goldContainer'

班级房间:

#pragma once
#include <SFML\Graphics.hpp>
#include "screenSettings.h"
#include "floorplanPatch.h"
#include "floorplanPatchContainer.h"
#include "enemyContainer.h"
#include "goldContainer.h"
class goldContainer;
class room{
public:
    room(int themenr, floorplanPatchContainer &f);
    void draw(sf::RenderWindow &window);
    int getStartPoint();
    int getEndPoint();
    void addFloorplanPatch(int x, int y, int type, floorplanPatch *patch);
    bool isSolid(sf::Vector2f position);
    void addEnemy();
    static const int FLOOR_TEXTURE1 = 0;
    static const int FLOOR_TEXTURE2 = 1;
    static const int FLOOR_TEXTURE3 = 2;
    static const int FLOOR_TEXTURE4 = 3;
    static const int WALL = 4;
    static const int OBSTACLE = 5;
    static const int COSMETIC = 6;
    int floorplan[xAs][yAs];
    enemyContainer* getEnemyContainer();
    void room::addEnemy(int health);
private:
    enemyContainer ec;
    int startPoint = 1 + rand() % (yAs - 2);
    int endPoint = 1 + rand() % (yAs - 2);
    sf::RectangleShape rectangle{ sf::Vector2f{ tileSizeX, tileSizeY } };
    sf::Texture wall;
    sf::Texture obstacle;
    sf::Texture floor1;
    sf::Texture floor2;
    sf::Texture floor3;
    sf::Texture floor4;
    sf::Texture cosmetic;
    void drawBackgroundTile(sf::RenderWindow &window, int i, int x, int y);
    goldContainer goldC;
};

第8行有class goldContainer;,否则会生成错误代码2146。

有人可能会解释如何解决这个错误和/或为什么会这样。

#pragma once
#include "gold.h"
#include "sound.h"
#include "player.h"
class player;
class room;
class goldContainer{
public:
    goldContainer();
    ~goldContainer();
    void checkPickedUp(player &player);
    void draw(sf::RenderWindow &window);
    void addGold(int amount, sf::Vector2f position, sf::Vector2f size);
    void clearAllGold();

private:
    std::vector<gold* > goldDrops;
    sound goldPickup{ "sounds\\goldPickup.wav" };
};

我认为这可能是一个循环的依赖性低谷:

^->goldContainer->player->roomContainer->room->|
|<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-< v

1 个答案:

答案 0 :(得分:2)

让我告诉你问题领域:

class Moon;

class Sun
{
    void Rotate(Moon);
};

现在,您实施Sun::Rotate,而不提供任何类声明(前向声明):

void Sun::Rotate(Moon m) // Error C2027
{

}

你能做什么:

  • 确保在Sun::Rotate进入编译阶段之前,声明Moon(即编译器现在知道)。您无需实现Moon的任何方法,只需声明即可。

示例:

class Moon;

class Sun
{
     void Rotate(Moon);
};

// Let it come by now
class Moon
{
public:
   void PleaseRotate();
};

// Moon is known
void Sun::Rotate(Moon m)
{
   m.PleaseRotate(); // It need not to be implemented by now.
}

请注意,Moon::PleaseRotate定义将由链接器解析,因此在 Sun::Rotate之前不需要的实现。