Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot be explicitly specialized

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..

like image 221
amanuel2 Avatar asked Jan 07 '23 04:01

amanuel2


1 Answers

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.

like image 174
Barry Avatar answered Jan 16 '23 18:01

Barry