C++ 同一命名空间中的相互依赖类问题

C++ 同一命名空间中的相互依赖类问题,c++,visual-c++,gcc,forward-declaration,circular-dependency,C++,Visual C++,Gcc,Forward Declaration,Circular Dependency,我真的陷入困境了。。。我需要移植代码,它有许多相互依赖的类,并使用名称空间来避免包含。这在MSVC中有效,但在GCC中我找不到处理这种情况的方法:( myString.h文件的内容: #include "baseBuffer.h" //I can't forward declare a base class, so I have to include its header namespace test { class MY_ALLOCATOR { static

我真的陷入困境了。。。我需要移植代码,它有许多相互依赖的类,并使用名称空间来避免包含。这在MSVC中有效,但在GCC中我找不到处理这种情况的方法:(

myString.h文件的内容:

#include "baseBuffer.h"
//I can't forward declare a base class, so I have to include its header

namespace test
{
    class MY_ALLOCATOR
    {
        static const unsigned int LIMIT = 4096;
        class myBuffer : public BaseBuffer<LIMIT>
        {
//...
        }
    };

    template <class T, typename ALLOC = MY_ALLOCATOR> class myContainer
    {
//...
    }

    typedef myContainer<char> myString;
}
#include "myObject.h"
//#include "myString.h"
//I can't include **myString.h**, because it includes this header file and I can't seem to find a way to use forward declaration of **myString** class...

namespace test
{
    template <uint limit> class BaseBuffer : public MyObject
    {
        public:
            myString sData;
//...
    }
}
#包括“baseBuffer.h”
//我不能向前声明基类,所以我必须包含它的头
名称空间测试
{
类MY_分配器
{
静态常量无符号整数限制=4096;
类myBuffer:PublicBaseBuffer
{
//...
}
};
模板类myContainer
{
//...
}
typedef myContainer myString;
}

baseBuffer.h文件的内容:

#include "baseBuffer.h"
//I can't forward declare a base class, so I have to include its header

namespace test
{
    class MY_ALLOCATOR
    {
        static const unsigned int LIMIT = 4096;
        class myBuffer : public BaseBuffer<LIMIT>
        {
//...
        }
    };

    template <class T, typename ALLOC = MY_ALLOCATOR> class myContainer
    {
//...
    }

    typedef myContainer<char> myString;
}
#include "myObject.h"
//#include "myString.h"
//I can't include **myString.h**, because it includes this header file and I can't seem to find a way to use forward declaration of **myString** class...

namespace test
{
    template <uint limit> class BaseBuffer : public MyObject
    {
        public:
            myString sData;
//...
    }
}
#包括“myObject.h”
//#包括“myString.h”
//我不能包含**myString.h**,因为它包含这个头文件,而且我似乎找不到一种方法来使用**myString**类的前向声明。。。
名称空间测试
{
模板类BaseBuffer:公共MyObject
{
公众:
myString sData;
//...
}
}


请帮助!

您的头文件中缺少保护

MSVC很可能允许您通过扩展来实现这一点。因此,有两种方法可以解决此问题:
1.第一种解决方案是合并这两个标题。
2.第二个解决方案是向前声明模板类myContainer,并在basebuffer.hpp中动态创建它(而不是创建
myString*sData
,而是创建
myString*sData

编辑
为baseBuffer添加一个cpp文件,并包含该文件而不是头文件。在头文件中,向前声明模板类myString,在源文件中,您可以包含任何您喜欢的内容。

您只需对其进行排序。myString(又称myContainer)如果使用myString作为存储的基础,则无法合理地使用BaseBuffer来分配其空间。如何“使用名称空间来避免包含”为什么要避免它们?它们对于C++独立编译模型是必不可少的。这些都是好的问题,但问题是这不是我的代码……你能发布一个小而可编译的文件子集来显示这个问题(编译在MSVC上,但在GCC上被破解)MSVC支持
#pragma once
,这可能是通常的做法。也就是说,我在他的代码中也看不到这些语句…谢谢,但你确定没有其他选择吗?Cody,#pragma once在这种情况下没有帮助,因为循环依赖性。别介意警卫,我想我不应该忽略它们。。。@Ryan Forward declare是避免循环依赖的标准方法。好的,看起来我必须使用您提到的第一个解决方案。