无法明确专门化

时间:2016-04-26 16:48:37

标签: c++ templates template-specialization

我收到此错误:

  

严重级代码描述项目文件行抑制状态   错误C2910'addingStuff :: addingStuffFunc':   不能明确   专门的BuckysTemplateSpecialization c:\ users \ amanuel \ documents \ visual   工作室   2015 \项目\ buckystemplatespecialization \ buckystemplatespecializ

当我尝试运行此代码时:

#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;
}

没有线索为什么......

1 个答案:

答案 0 :(得分:8)

你只想:

std::string addingStuff<std::string, std::string>::addingStuffFunc() {
    return "Sorry.. Adding strings is Illegal!!";
}

额外的template <>被解释为成员函数的模板ID,代码被解释为<>的明确特征。但是addingStuffFunc不是函数模板,因此是错误。