gcc error:Too Few Template Parameter Lists

14 views
Skip to first unread message

LeoReis

unread,
Apr 7, 2008, 5:16:30 AM4/7/08
to 人生难得几回搏
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;
}

LeoReis

unread,
Apr 7, 2008, 11:58:23 PM4/7/08
to 人生难得几回搏
另外需要注意的一点是,静态变量不光要定义实体,还要给它赋初值,否则可能link的时候报错,undefined reference to
xxx
Reply all
Reply to author
Forward
0 new messages