Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ GCC错误:';X';不是模板吗?_C++_Templates_Gcc - Fatal编程技术网

C++ GCC错误:';X';不是模板吗?

C++ GCC错误:';X';不是模板吗?,c++,templates,gcc,C++,Templates,Gcc,最近,我在Codepad上测试了一些模板代码。虽然代码是正确的,但GCC给了我一个非常奇怪的错误。我还在Ideone上进行了测试: 测试代码: template<int num> struct count; template<> struct count<-1> { }; int main() { return 0; } 而GCC 4.3.4编译正常,没有错误。 这真的是一个编译器错误,还是我的代码不正确(由于扩展而编译? template<in

最近,我在Codepad上测试了一些模板代码。虽然代码是正确的,但GCC给了我一个非常奇怪的错误。我还在Ideone上进行了测试:

测试代码:

template<int num>
struct count;

template<>
struct count<-1>
{
};

int main()
{
 return 0;
}
而GCC 4.3.4编译正常,没有错误。 这真的是一个编译器错误,还是我的代码不正确(由于扩展而编译?

template<int num>
struct count;

template<>
struct count<-1>
{
};

int main()
{
 return 0;
}

因此,我猜测Codepad正在强制您执行一个前导码,从而触发错误。

看起来Codepad使用名称空间std无声地添加了一组
#include
和一个
是否需要:查看哪些不应自行编译:

int main()
{
    cout << "Hello" << endl;
}

Output: Hello
intmain()
{

cout此错误最可能的原因是标识符名称之间的冲突

有时,一个类会从一个类开始。该类会在一段时间后扩展到模板中。当此时仍然存在类原型时(例如,在不同的头文件中),编译器会发出此错误


(虽然这不适用于此处的特定情况,但此答案可能会帮助其他在搜索帮助时访问此页面的人。)

只需知道,您是否包括STL+使用命名空间std?因为std::count存在。count带有小写“c”作为结构名不是一个好的编程方法practice@MoatazElmasry这是谁的?这是许多项目中使用的约定,包括C++标准库。我没有GCC 4.1.2的问题。我只是在这里测试,设计用来与C++ 03一起工作。对于GCC 4.1.2:有时我喜欢使用CODEPad来测试简单代码,并且它正在使用。gcc 4.1.2你似乎发现了“错误”。干得好!+1+答案。这肯定是代码板应该显示的东西。。。
#include <algorithm>
using namespace std;

template<int num>
struct count;

template<>
struct count<-1>
{
};

int main()
{
 return 0;
}
count.cpp:8: error: 'count' is not a template
compilation terminated due to -Wfatal-errors.
int main()
{
    cout << "Hello" << endl;
}

Output: Hello