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
You’re getting this error because there isn’t a way to unpack your
pinto eitherinitfunction. In your instantiationMystrcat<char*, char *>, unpacking aP...will yield a single thing in the type:char*, which doesn’t have aninitwith 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
initwill 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: