【发布时间】:2014-01-06 21:28:56
【问题描述】:
大家好,我有两个类都需要包含另一个类,我试图在 A.h 文件中包含 B 类标头,在 B.h 文件中包含 A 类标头,但由于循环包含,我得到了错误,经过一些研究我找到了称为前向声明的解决方案,但对我来说不起作用,这是我的实际情况:
// Game.h
#ifndef _GAME_H_
#define _GAME_H_
#ifndef _GRAPHICSDEVICE_H_
#include "GraphicsDevice.h"
#endif
using namespace BSGameFramework::Graphics;
namespace BSGameFramework
{
public ref class Game
{
public:
Game();
virtual ~Game();
property GraphicsDevice^ Device
{
GraphicsDevice^ get()
{
return device_;
}
}
// Other code here
private:
GraphicsDevice^ device_;
}
}
#endif
这是 GraphicsDevice 类:
// GraphicsDevice.h
#ifndef _GRAPHICSDEVICE_H_
#define _GRAPHICSDEVICE_H_
// forward declaration
ref class Game; // <-- this is giving me error C2872: 'Game' : ambiguous symbol
using namespace BSGameFramework;
namespace BSGameFramework
{
namespace Graphics
{
public ref class GraphicsDevice
{
public:
GraphicsDevice(); // I need to pass my Game class to the constructor
virtual ~GraphicsDevice();
// Here other code
}
}
}
有人可以帮帮我吗?我快疯了xD
已解决
我已经解决了替换 ref 类 Game;带有命名空间 BSGameFramework { ref class Game; }
【问题讨论】:
-
前向声明“不起作用”是什么意思?您是否收到任何错误消息?
-
在您的代码中的其他地方还有另一个名为“Game”的标识符。我们看不到它。
-
请读到最后,我已经把cmets放在代码上!
-
@HansPassant 不,没有,如果我删除 ref 类 Game;一切都会好起来的!
标签: .net c++-cli circular-reference