Friend classes across different namespaces. Is that possible

前端 未结 2 1109
北恋
北恋 2021-01-01 12:10

I\'m having problems trying to use the friend feature of C++. I have these interfaces:

#pragma once
#include \"Mesh3D.h\"
#include 
namespace t         


        
相关标签:
2条回答
  • 2021-01-01 12:13

    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);
            };
    
        };    
    }
    
    0 讨论(0)
  • 2021-01-01 12:31

    I guess you need to remove following code in the second file:

    #include "GeometryManager.h"
    
    class CGeometryManager3D;
    

    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;

    0 讨论(0)
提交回复
热议问题