我正在使用gcc。 我想创建一个我自己的数据类型的队列。
在下面的代码中,当我在main()之外声明struct
时,它工作正常但在内部定义struct
时会产生编译时错误。
#include <queue>
using namespace std;
int main()
{
struct tempPos {int a; int b;}; //....(1)
queue<tempPos> b; //works only if tempPos is defined outside main
queue<int> x; //works fine anyways
return 0;
}
以下是错误。
test.cpp: In function ‘int main()’:
test.cpp:10:15: error: template argument for ‘template<class _Tp> class std::allocator’ uses local type ‘main()::tempPos’
test.cpp:10:15: error: trying to instantiate ‘template<class _Tp> class std::allocator’
test.cpp:10:15: error: template argument 2 is invalid
test.cpp:10:18: error: invalid type in declaration before ‘;’ token
Compilation failed.
答案 0 :(得分:0)
C ++禁止将本地定义的类与模板一起使用,因为它们没有链接。标准说:
14.3.1 / 2:。本地类型,没有链接的类型,未命名的类型或从这些类型中复合的类型不得用作模板参数模板类型参数。