跨不同命名空间的友元类。这可能吗
我在尝试使用 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)。 可以解决这个问题吗?
编辑:上面的代码是一个模拟代码,用于显示我的问题。您的解决方案对该代码运行良好,但当我在实际代码中付诸实践时却不起作用。真实的代码几乎相同:
#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:我已经通过在两个类中转发每个类及其自己的命名空间的声明来解决这个问题。我认为这应该通过循环声明而失败,但它最终完美地工作了。
感谢大家的帮助。
I'm having problems trying to use the friend feature of C++. I have these interfaces:
#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);
};
};
};
and
#pragma once
#include "GeometryManager.h"
class CGeometryManager3D;
namespace render{
class CMesh3D
{
public:
friend class tools::sysInput::CGeometryManager3D;
CMesh3D(void);
~CMesh3D(void);
};
};
I don't know what is happening, but a lot of errors are thrown by the compiler (Visual C++ 2008). Is it possible to solve this?
edit: Above code is a mock code to show my problem. Your solution works fine to this code but when I put in practice in my real code didn't work. The real code is neearly the same:
#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_
and
#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_
The compiler return an error that says "CMesh3D" is not a member of render.
Again, any help is welcome. :)
edit 2: I've solved it by forwarding declaration of each class and its own namespace in both classes. I thought that this should fail by circular declaration, but it finally work perfectly.
Thanks to all for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看看这样的东西是否工作得更好一些(目前,我已将它们合并到一个源文件中)。
See if something like this works a bit better (for the moment, I've merged them into a single source file).
我想您需要删除第二个文件中的以下代码:
第一行导致循环包含,如问题中的注释所示;
第二行声明了一个与全局名称空间完全不相关的类;
I guess you need to remove following code in the second file:
The first line causes circular inclusion as the comments in the question suggests;
The second line declares a totally irrelevant class as it is in the global name space;