模板类中增加了一个特化的模板函数,编译时发生“错误: 模板参数表太多”错误!

fibbery 2008-10-14 10:02:47
template <typename T1,typename T2>
class CMyTemplate
{
public:
template<typename _Ty>
void func(const T1 & t1,const T2, _Ty t)//错误: 模板参数表太多
{
....;
}
};

上面是我具体的代码的精简版,在vs2008中编译没有问题,在gcc 版本 4.2.3 (Ubuntu 4.2.3-2ubuntu7)中编译发生如题错误!
请问如何解决?多谢!
...全文
584 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
fibbery 2008-10-21
  • 打赏
  • 举报
回复
问题解决了,其实很简单,对于全特化,完全没有必要,只需要编写一个正常的函数来重载模版函数即可!
主要的问题出在:
template <> void p_func<std::string>(const std::string & ty);

改成:
void p_func(const std::string & ty);

这样在g++和VC9中都没有问题!

感谢各位,尤其是baihacker
cryengine 2008-10-20
  • 打赏
  • 举报
回复
xuexi
baihacker 2008-10-19
  • 打赏
  • 举报
回复
18 In an explicit specialization declaration for a member of a class template or a member template that appears
in namespace scope, the member template and some of its enclosing class templates may remain unspecial-
ized, except that the declaration shall not explicitly specialize a class member template if its enclosing class
templates are not explicitly specialized as well.
在类模板的成员或者成员模板的显示特化声明出现在命空间的时候,模板成员和内附它的类模板是可能没有特化的,
除了在内附一个模板成员的类模板还没有显示特化的时候,声明中不允许显示特化这个类的模板成员
In such explicit specialization declaration, the keyword
template followed by a template-parameter-list shall be provided instead of the template<> preced-
ing the explicit specialization declaration of the member. The types of the template-parameters in the
template-parameter-listshall be the same as those specified in the primary template definition.
[Example:
template<class T1> class A {
template<class T2> class B {
template<class T3> void mf1(T3);
void mf2();
};
};
template<> template<class X>
class A<int>::B { };
template<> template<> template<class T>
void A<int>::B<double>::mf1(T t) { }
template<class Y> template<>
void A<Y>::B<double>::mf2() { } // ill-formed; B<double> is specialized but
// its enclosing class template A is not
!end example]
帅得不敢出门 2008-10-19
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 zmlovelx 的回复:]
引用 6 楼 pacman2000 的回复:
用g++试了,显然没有出错。
为啥我的G++ 出错了
[/Quote]
貌似两个问题 我晕
是不是可以把模板函数从模板类中单独抽取出来做成非成员friend函数
帅得不敢出门 2008-10-19
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 pacman2000 的回复:]
用g++试了,显然没有出错。
[/Quote]
为啥我的G++ 出错了
baihacker 2008-10-19
  • 打赏
  • 举报
回复
A member or a member template of a class template may be explicitly specialized for a given implicit
instantiation of the class template
, even if the member or member template is defined in the class template
definition.
baihacker 2008-10-19
  • 打赏
  • 举报
回复
16 A member or a member template of a class template may be explicitly specialized for a given implicit
instantiation of the class template, even if the member or member template is defined in the class template
definition. An explicit specialization of a member or member template is specified using the template spe-
cialization syntax.
 [Example:
template<class T> struct A {
void f(T);
template<class X1> void g1(T, X1);
template<class X2> void g2(T, X2);
void h(T) { }
};
// specialization
template<> void A<int>::f(int);
// out of class member template definition
template<class T> template<class X1> void A<T>::g1(T, X1) { }
// member template specialization
template<> template<class X1> void A<int>::g1(int, X1);
// member template specialization
template<> template<>
void A<int>::g1(int, char); // X1 deduced as char
template<> template<>
void A<int>::g2<char>(int, char); // X2 specified as char
// member specialization even if defined in class definition
template<> void A<int>::h(int) { }
!end example]
帅得不敢出门 2008-10-19
  • 打赏
  • 举报
回复
似乎解决不了 等GCC升级吧
帅得不敢出门 2008-10-19
  • 打赏
  • 举报
回复
这个GCC不行啊
baihacker 2008-10-19
  • 打赏
  • 举报
回复
模板类的模板成员,在VC9中可以全特化,G++不认.
fibbery 2008-10-19
  • 打赏
  • 举报
回复
下面的是增加了行号的代码:
01   : #include <string>
02 : #include <iostream>
03 :
04 : template <typename T1,typename T2>
05 : class CMyTemplate
06 : {
07 : template <typename _Ty>
08 : void p_func(const _Ty & ty)
09 : {
10 : std::cout<<"const _Ty & ty"<<std::endl;
11 : }
12 : template <>
13 : void p_func<std::string>(const std::string & ty)
14 : {
15 : std::cout<<"const std::string & ty"<<std::endl;
16 : }
17 : public:
18 : void my_func(const T1 & t1,const T2 & t2)
19 : {
20 : p_func(t2);
21 : std::cout<<"const T1 & t1,const T2 & t2"<<std::endl;
22 : }
23 : template <typename _Ty>
24 : void my_func(const T1 & t1,const _Ty & ty)
25 : {
26 : p_func(ty);
27 : std::cout<<"const T1 & t1,const _Ty & ty"<<std::endl;
28 : }
29 : };
30 :
31 : int main(int argc, char * argv[])
32 : {
33 : std::string str="abc";
34 : CMyTemplate<int,int> my_int;
35 : my_int.my_func(1,2);
36 : my_int.my_func(1,str);
37 : return 0;
38 : }
39 :
fibbery 2008-10-19
  • 打赏
  • 举报
回复
终于有时间继续讨论这个问题了!
我的代码如下:

#include <string>
#include <iostream>

template <typename T1,typename T2>
class CMyTemplate
{
template <typename _Ty>
void p_func(const _Ty & ty)
{
std::cout<<"const _Ty & ty"<<std::endl;
}
template <>
void p_func<std::string>(const std::string & ty)
{
std::cout<<"const std::string & ty"<<std::endl;
}
public:
void my_func(const T1 & t1,const T2 & t2)
{
p_func(t2);
std::cout<<"const T1 & t1,const T2 & t2"<<std::endl;
}
template <typename _Ty>
void my_func(const T1 & t1,const _Ty & ty)
{
p_func(ty);
std::cout<<"const T1 & t1,const _Ty & ty"<<std::endl;
}
};

int main(int argc, char * argv[])
{
std::string str="abc";
CMyTemplate<int,int> my_int;
my_int.my_func(1,2);
my_int.my_func(1,str);
return 0;
}


错误信息以及g++版本信息:
引用
mytemplate.cpp:12: 错误: 显式特例化出现在非命名空间作用域‘class CMyTemplate<T1, T2>’中
mytemplate.cpp:24: 错误: 模板参数表太多
mytemplate.cpp: In function ‘int main(int, char**)’:
mytemplate.cpp:36: 错误: 对‘CMyTemplate<int, int>::my_func(int, std::string&)’的调用没有匹配的函数
mytemplate.cpp:18: 附注: 备选为: void CMyTemplate<T1, T2>::my_func(const T1&, const T2&) [with T1 = int, T2 = int]


g++版本信息如下:

使用内建 specs。
目标:x86_64-linux-gnu
配置为:../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
线程模型:posix
gcc 版本 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

wangdeqie 2008-10-14
  • 打赏
  • 举报
回复

//估计是别的地方错了,偶试了这段代码没问题

#include <cstdlib>
#include <iostream>

using namespace std;

template <typename T1,typename T2>
class CMyTemplate
{
public:
template<typename _Ty>
void func(const T1 & t1,const T2, _Ty t)
{
//....;
}
};

int main(int argc, char *argv[])
{
system("PAUSE");
return EXIT_SUCCESS;
}

xf_pan 2008-10-14
  • 打赏
  • 举报
回复
为什么不放一起定义。。
template <typename T1,typename T2,typename _Ty>
taodm 2008-10-14
  • 打赏
  • 举报
回复
贴全代码和原始编译信息。
你的代码和标题根本对不上号。
yshuise 2008-10-14
  • 打赏
  • 举报
回复
dev也没错啊
pacman2000 2008-10-14
  • 打赏
  • 举报
回复
用g++试了,显然没有出错。
fibbery 2008-10-14
  • 打赏
  • 举报
回复
等我再试一试

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧