I get this error:
Severity Code Description Project File Line Suppression State Error C2910 'addingStuff::addingStuffFunc': cannot be explicitly specialized BuckysTemplateSpecialization c:\users\amanuel\documents\visual studio 2015\projects\buckystemplatespecialization\buckystemplatespecializ
When I try to run this code:
#include<iostream>
#include<string>
template<class F, class S>
class addingStuff {
public:
addingStuff(F fCons, S sCons) : f(fCons), s(sCons){}
F addingStuffFunc();
private:
F f; S s;
};
template<class F, class S>
F addingStuff<F, S>::addingStuffFunc() {
return(f + s);
}
template<>
class addingStuff<std::string , std::string>{
public:
addingStuff(std::string sConst, std::string s2Const):s(sConst), s2(s2Const){}
std::string addingStuffFunc();
private:
std::string s, s2;
};
template<>
std::string addingStuff<std::string, std::string>::addingStuffFunc() {
return "Sorry.. Adding strings is Illegal!!";
}
int main() {
addingStuff<std::string, std::string> exampleStuff("Hello " , "World");
std::cout << exampleStuff.addingStuffFunc() << std::endl;
}
No Clue Why..
You just want:
std::string addingStuff<std::string, std::string>::addingStuffFunc() {
return "Sorry.. Adding strings is Illegal!!";
}
The extra template <>
gets interpreted as the template-id for the member function, and the code gets interpreted as an explicit specialiation of it for <>
. But addingStuffFunc
isn't a function template, hence the error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With