可变参数模板 - 编译错误
你能帮我修复以下代码中的编译错误吗:
#include <sstream>
#include <iostream>
using namespace std;
template<typename T, typename ...P>
class Mystrcat{
public:
Mystrcat(T t, P... p){init(t,p...);}
ostringstream & get(){return o;}
private:
ostringstream o;
void init(){}
void init(T t, P... p);
};
template<typename T, typename ...P>
void Mystrcat<T,P...>::init(T t, P ...p){
o << t;
if (sizeof...(p)) init(p...);
}
int main(){
Mystrcat<char*,char *> m("Amma","Namasivayah");
cout << m.get().str();
}
我收到错误,没有匹配的函数用于调用注意
‘Mystrcat<char*, char*>::init(char*&)’
:候选者是:
void Mystrcat<T, P>::init() [with T = char*, P = char*]
void Mystrcat<T, P>::init(T, P ...) [with T = char*, P = char*]
gcc版本4.4.3(Ubuntu 4.4.3-4ubuntu5)
谢谢 苏雷什
Could you help me in fixing the compile error in the following code:
#include <sstream>
#include <iostream>
using namespace std;
template<typename T, typename ...P>
class Mystrcat{
public:
Mystrcat(T t, P... p){init(t,p...);}
ostringstream & get(){return o;}
private:
ostringstream o;
void init(){}
void init(T t, P... p);
};
template<typename T, typename ...P>
void Mystrcat<T,P...>::init(T t, P ...p){
o << t;
if (sizeof...(p)) init(p...);
}
int main(){
Mystrcat<char*,char *> m("Amma","Namasivayah");
cout << m.get().str();
}
I get the error, no matching function for call to
‘Mystrcat<char*, char*>::init(char*&)’
note: candidates are:
void Mystrcat<T, P>::init() [with T = char*, P = char*]
void Mystrcat<T, P>::init(T, P ...) [with T = char*, P = char*]
gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
Thanks
suresh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到此错误是因为无法将
p
解压到任一init
函数中。在实例化Mystrcat
中,解压P...
将产生以下类型的单个内容:char*
,它没有带有该签名的init
(实例化版本将具有void init()
和void init(char*, char*)
,而您正在尝试调用init(char*)
)。事实上,你的模板是不可能实例化的,因为
init
总是比你在void Mystrcat::init(T t 中给出的参数多一个参数,P ...p)
。如果您更改定义以调用您定义的内容:那么这将起作用(至少在 g++-4.5.2 中)。
编辑:我认为这就是您真正想要的:
You're getting this error because there isn't a way to unpack your
p
into eitherinit
function. In your instantiationMystrcat<char*, char *>
, unpacking aP...
will yield a single thing in the type:char*
, which doesn't have aninit
with that signature (the instantiated version will have avoid init()
andvoid init(char*, char*)
, whereas you are trying to callinit(char*)
).In fact, your template is impossible to instantiate, since
init
will always take one more argument than you give it invoid Mystrcat<T,P...>::init(T t, P ...p)
. If you change the definition to call what you have defined:then this will work (at least in g++-4.5.2).
EDIT: I think this is what you're actually looking for: