同一命名空间中相互依赖的类问题
我正在真正修复...我需要移植代码,其中有很多相互依赖的类并使用命名空间以避免包含。这在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的头文件中缺少守卫。
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
, createmyString *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.