C++/CLI 循环内含物



大家好,我有两个类,都需要包含另一个类,我试图在 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

解决

我已经解决了用命名空间BSGameFramework { ref class Game; }替换ref类Game的问题

已解决

我已经解决了用命名空间BSGameFramework { ref class Game; }替换ref类Game的问题