Friend classes across different namespaces. Is that possible

Killrazor picture Killrazor · Oct 2, 2010 · Viewed 21.8k times · Source

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.

Answer

Jerry Coffin picture Jerry Coffin · Oct 2, 2010

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

    };    
}