我有問題試圖使用C++的朋友功能。我有這些接口:跨不同名稱空間的朋友類。這是可能的
#pragma once
#include "Mesh3D.h"
#include <string>
namespace tools{
namespace sysInput{
class CGeometryManager3D
{
public:
bool loadFromFile(render::CMesh3D& mesh, std::string filename);
CGeometryManager3D(void);
~CGeometryManager3D(void);
};
};
};
和
#pragma once
#include "GeometryManager.h"
class CGeometryManager3D;
namespace render{
class CMesh3D
{
public:
friend class tools::sysInput::CGeometryManager3D;
CMesh3D(void);
~CMesh3D(void);
};
};
我不知道發生了什麼,但很多的錯誤是由編譯器拋出(VISUAL C++ 2008)。有可能解決這個問題嗎?
編輯:上面的代碼是一個模擬代碼來顯示我的問題。您的解決方案對此代碼運行良好,但是當我在實際代碼中進行實踐時無法正常工作。真正的代碼是neearly相同:
#ifndef _ZELESTE_IO_GEOMETRY_MANAGER_H_
#define _ZELESTE_IO_GEOMETRY_MANAGER_H_
#include "ResourceLocationManager.h"
#include <string>
#include "../../render/C3DMesh.h"
namespace tools{
namespace sysInput{
class CGeometryManager
{
private:
CGeometryManager(void);
~CGeometryManager(void);
static CGeometryManager* m_instance;
public:
static CGeometryManager* getInstance();
bool load3DGeometryFromFile(render::C3DMesh* mesh, const std::string filename);
};
};
};
#endif //_ZELESTE_IO_GEOMETRY_MANAGER_H_
和
#ifndef _C3D_MESH_H_
#define _C3D_MESH_H_
#include "Mesh.h"
#include "../tools/io/GeometryManager.h"
#include <string>
namespace tools{
namespace sysInput{
class CGeometryManager;
}
}
namespace render{
class C3DMesh
:public CMesh
{
public:
friend class tools::sysInput::CGeometryManager;
C3DMesh(void);
~C3DMesh(void);
};
};
#endif // _C3D_MESH_H_
編譯器返回,上面寫着「CMesh3D」不是渲染的成員的錯誤。 再次,任何幫助是值得歡迎的。 :)
編輯2:我已經通過轉發每個類和它們自己的命名空間在這兩個類中的聲明來解決它。我認爲這應該通過循環聲明失敗,但它終於完美地工作。
感謝大家的幫助。
第一個文件名爲'GeometryManager.h',第二個文件名爲'Mesh3D.h'?如果是這樣,你有一個通用的包含問題。 – 2010-10-01 23:46:15
@詹姆斯,這是我的想法。 – 2010-10-01 23:48:39
不......不會出現馬戲團包容。在包含B和B包含A的情況下,您可以通過將其中一個類的聲明轉發到另一個類中來解決循環包含,就像我在代碼中所做的那樣。 – Killrazor 2010-10-03 10:16:20