模板、静态变量等的声明冲突
我有这个源代码,我想在 g++ 4.4.5 下编译。此代码可以在 Visual C++ 2008 中正确编译,但不能在 g++ 中编译。
#include <iostream>
template<typename T, int MAXSIZE>
class ThreadSafePool
{
typedef T theType;
};
template<int value>
class CNetPacket
{
public:
static const int s_max_pool_cnt=30;
private:
static ThreadSafePool<CNetPacket<value>, CNetPacket<value>::s_max_pool_cnt> s_packet_pool;
};
template<int value>
ThreadSafePool<CNetPacket<value>, CNetPacket<value>::s_max_pool_cnt> CNetPacket<value>::s_packet_pool;
int main()
{
int temp = CNetPacket<300>::s_max_pool_cnt;
}
g++ 给出以下错误消息:
test.cpp:21: error: 冲突声明 ThreadSafePool, CNetPacket::s_max_pool_cnt> CNetPacket::s_packet_pool
test.cpp:16: 错误:CNetPacket::s_packet_pool 先前声明为 ThreadSafePool,30> CNetPacket::s_packet_pool
test.cpp:21: 错误:ThreadSafePool 的声明,30>类外的 CNetPacket::s_packet_pool 不是定义
任何帮助将不胜感激。谢谢。
I have this source code that I want to compile under g++ 4.4.5. This code compiles properly in Visual C++ 2008 but not with g++.
#include <iostream>
template<typename T, int MAXSIZE>
class ThreadSafePool
{
typedef T theType;
};
template<int value>
class CNetPacket
{
public:
static const int s_max_pool_cnt=30;
private:
static ThreadSafePool<CNetPacket<value>, CNetPacket<value>::s_max_pool_cnt> s_packet_pool;
};
template<int value>
ThreadSafePool<CNetPacket<value>, CNetPacket<value>::s_max_pool_cnt> CNetPacket<value>::s_packet_pool;
int main()
{
int temp = CNetPacket<300>::s_max_pool_cnt;
}
g++ gives this error message:
test.cpp:21: error: conflicting declaration ThreadSafePool, CNetPacket::s_max_pool_cnt> CNetPacket::s_packet_pool
test.cpp:16: error: CNetPacket::s_packet_pool has a previous declaration as ThreadSafePool, 30> CNetPacket::s_packet_pool
test.cpp:21: error: declaration of ThreadSafePool, 30> CNetPacket::s_packet_pool outside of class is not definition
Any help would be appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于我来说,这对 gcc 4.7 svn、gcc 4.6.1、gcc 4.5.3、gcc 4.3.4 编译得很好,但对 gcc 4.4.2 则失败了,
我认为这是一个编译器错误。
This compiles fine for me with gcc 4.7 svn, gcc 4.6.1, gcc 4.5.3, gcc 4.3.4 and fails with gcc 4.4.2
I think it is a compiler bug.