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

发布于 2024-11-06 12:37:54 字数 1076 浏览 1 评论 0原文

我正在真正修复...我需要移植代码,其中有很多相互依赖的类并使用命名空间以避免包含。这在 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;
}


baseBuffer.h 文件的内容:

#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;
//...
    }
}


请帮忙!

I'm in a real fix... I need to port code, which has a lot of interdependent classes and uses namespaces in order to avoid includes. This works in MSVC, but I can't find a way to deal with this situation in GCC :(

Contents of myString.h file:

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



Contents of baseBuffer.h file:

#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;
//...
    }
}



Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑脸一如从前 2024-11-13 12:37:54

您的头文件中缺少守卫。

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

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

You are missing guards in your header files.

MSVC most likely allows you to do that through an extension. Therefore, there are two ways how to solve this :
1. the 1st solution is to merge those two headers.
2. the 2nd solution is forward declare template class myContainer, and create it dynamically in the basebuffer.hpp (instead of myString sData, create myString *sData)

EDIT
Add a cpp file for baseBuffer, and include that instead of the header file. In the header forward declare template class myString, and in the source you can include whatever you like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文