linux下用gcc编译项目碰到一个错误,信息为Too Few Template Parameter Lists
具体表现在模板类的静态成员变量的定义上。经查是因为没有严格按照c++标准的方式来做。
链接
http://c2.com/cgi/wiki?TooFewTemplateParameterLists
内容如下,讲的很清楚:
TooFewTemplateParameterLists is an error message produced by newer
versions of the GnuCpp compiler for code which previously compiled.
The problem concerns the initialization of static members of a
templated class. Previously it was sufficient to declare the member,
but this now has to be preceded by template <>. The difficulty is that
the error message says what the problem is in such a way as not to
provide a clue as to what to do next. See Example code 1.
CppTemplatesTheCompleteGuide gives a different example using a
template class with a typed template parameter. See Example code 2.
The examples have been run with GnuCpp 4.0.2. -- JohnFletcher
--------------------------------------------------------------------------------
Example code 1
template <class T>
class A
{
static int a;
static const char * const name;
};
// int A<int>::a = 0; This now fails with "error: too few template-
parameter-lists"
// This conforms to the CeePlusPlus standard, which older versions of
the compiler do not enforce.
template<> int A<int>::a = 0;
template<> const char * const A<int>::name = "example";
int main()
{
return 0;
}
--------------------------------------------------------------------------------
Example code 2
#include <iostream>
template <int I>
class B
{
public:
static int b;
};
template<> int B<1>::b = 1;
template <int I> int B<I>::b = 0;
int main()
{
std::cout << B<1>::b << std::endl; // 1
std::cout << B<2>::b << std::endl; // 0
return 0;
}