不同命名空间的朋友类。那可能吗 [英] Friend classes across different namespaces. Is that possible

查看:202
本文介绍了不同命名空间的朋友类。那可能吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试使用C ++的朋友功能时遇到问题。我有这些接口:

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);
        };

    };

};

#pragma once
#include "GeometryManager.h"

class CGeometryManager3D;
namespace render{

    class CMesh3D
    {
    public:
        friend class tools::sysInput::CGeometryManager3D;
        CMesh3D(void);
        ~CMesh3D(void);
    };

};

我不知道发生了什么,但是编译器抛出了很多错误C ++ 2008)。这可以解决这个问题吗?

I don't know what is happening, but a lot of errors are thrown by the compiler (Visual C++ 2008). It is 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_

#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不是渲染的成员。
再次欢迎任何帮助。 :)

The compiler return an error that says "CMesh3D" is not a member of render. Again, any help is welcome. :)

编辑2:我已经解决了它通过转发每个类的声明和自己的命名空间在这两个类。我认为这应该通过循环声明,但它终于完美地工作。

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.

感谢所有的帮助。

推荐答案

看看这样的效果是否更好(目前,我已经将它们合并到一个单一的源文件)。

See if something like this works a bit better (for the moment, I've merged them into a single source file).

#include <string>

namespace tools {
namespace sysInput {
class CGeometryManager3D;
}
}

namespace render {

    class CMesh3D
    {
    public:
        friend class tools::sysInput::CGeometryManager3D;
        CMesh3D(void);
        ~CMesh3D(void);
    };

};

namespace tools{
    namespace sysInput{
        class CGeometryManager3D
        {
        public:
            bool loadFromFile(render::CMesh3D& mesh, std::string filename);
            CGeometryManager3D(void);
            ~CGeometryManager3D(void);
        };

    };

};

这篇关于不同命名空间的朋友类。那可能吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆