返回信息流template<class C, class T = C*> class Print{
public:
void operator()(C x){
cout<<x<<endl;
}
void test();
};
template<class C, class T = C*> void Print<C, T>::test()
{
cout<<"test"<<endl;
}
编译报错:default template arguments may not be used in function templates
函数模板不能使用默认模板参数,我是知道的。
但是类成员函数在类声明的外部实现,也不能带上默认模板参数?
那带默认模板参数的类,它的成员函数只能在类声明里实现吗?
请求各位大牛。
这是一条镜像帖。来源:北邮人论坛 / cpp / #73688同步于 2013/9/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
CPP机器人发帖
【求指导】关于类成员函数的默认模板参数问题
liuwaiting
2013/9/7镜像同步3 回复
订阅后,新回复会通过你的通知中心匿名送达。
3 条回复
template<class C, class T> void Print<C, T>::test()
{
cout<<"test"<<endl;
}
原来这样就可以 = = 坑爹呢
#include<iostream>
#include<string>
using namespace std;
template<class C, class T = C*> class Print{
public:
typedef C mapped_type;
//x is string, not iterator
void operator()(C x){
cout<<x<<endl;
}
mapped_type& test(C x, T y);
};
template<class C, class T> Print<C,T>::mapped_type& Print<C, T>::test(C x, T y)
{
cout<<"Test !!!!!:"<<*y<<endl;
return C;
}
int main()
{
int a = 1;
int b = 2;
Print<int> o;
o(a);
o.test(a,&b);
}
test.cpp:16: error: expected constructor, destructor, or type conversion before ‘&’ token
这个问题呢? 有人了解吗?
template<class C, class T> typename Print<C,T>::mapped_type& Print<C, T>::test(C x, T y)
原来要用typename指明类中typedef中定义的类型,擦。