cjh*_*cjh 4 c++ templates specialization
以下代码工作正常,一个带有定义和用法的简单模板类
#include <string>
#include <iostream>
using namespace std;
template<class T> class foo{
public:
string what();
};
template<class T> string foo<T>::what(){
return "foo of type T";
}
int main(){
foo<int> f;
cout << f.what() << endl;
}
Run Code Online (Sandbox Code Playgroud)
如果我然后添加以下内容(上面的主要,但在声明模板类foo之后;)
template<> class foo<char>{
public:
string what();
};
template<> string foo<char>::what(){
return "foo of type char";
}
Run Code Online (Sandbox Code Playgroud)
我从g ++收到错误
第19行:错误:模板ID'什么<>'为'std :: string foo :: what()'与任何模板声明都不匹配
这是一个显示错误的键盘:http://codepad.org/4HVBn9oJ
我做了什么明显的错误?或者使用c ++模板是不可能的?是否可以定义所有内联方法(使用模板<> foo的定义)?
再次感谢所有人.
template<> class foo<char>{
public:
string what();
};
/*template<>*/ string foo<char>::what(){
return "foo of type char";
}
Run Code Online (Sandbox Code Playgroud)
你不需要那个template<>.foo<char>在它专业化之后已经是一个完整的类型.