我不明白爲什麼我在這個特定的例子中得到一個無法解析的外部符號。不明白爲什麼我得到一個無法解析的外部符號?
我在頭文件中定義了一個Rectangle結構。
我有一個Level類,它有一個指向Player類的指針,Level創建一個新玩家。
我的Player類,構造函數接受一個指向它的當前級別的指針。
因此,因爲我的關卡包含Player.h
我在我的Player類中聲明瞭Level以防止循環引用。
但是,當我嘗試向我的Player類添加一個Rectangle成員變量時 - 我得到了無法解析的外部符號錯誤,但我看不到爲什麼 - 因爲Player類包含頭 - 並且定義在頭。它在我的Level類中工作正常。
此外 - 如果我將成員變量更改爲指針,它鏈接正常。
有人可以向我解釋爲什麼會出現這種情況,我該如何解決這個問題?
// Rectangle.h
#pragma once
namespace Test
{
struct Rectangle
{
public:
Rectangle();
Rectangle(
int x,
int y,
int width,
int height) :
X(x),
Y(y),
Width(width),
Height(height)
{
}
int X;
int Y;
int Width;
int Height;
};
}
// Level.h
#pragma once
#include "Player.h"
#include <memory>
namespace Test
{
class Level
{
public:
Level()
{
_player.reset(new Player(this));
};
private:
std::unique_ptr<Player> _player;
};
}
// Player.h
#pragma once
#include "Rectangle.h"
namespace Test
{
class Level;
class Player
{
public:
Player(
Level* currentLevel) :
_currentLevel(currentLevel)
{
}
private:
Level* _currentLevel;
// This line causes the issue
Rectangle _localBounds;
};
}
顯示你得到的錯誤?你永遠不會定義Rectangle,AFAICS的默認構造函數。 – 2015-04-05 08:32:42
'矩形(){}' – 2015-04-05 08:34:13
哦,我的上帝。夥計們。謝謝。我怎麼錯過了。有人創建回覆,我會將其標記爲已接受。我是這樣一個doofus。 – Terry 2015-04-05 08:40:06