下面的代码工作正常,一个简单的模板类的定义和使用C++模板专业化方法定义
#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;
}
如果我然后添加以下(以上为主,但模板类foo的声明之后; )
template<> class foo<char>{
public:
string what();
};
template<> string foo<char>::what(){
return "foo of type char";
}
我得到一个错误从克++
Line 19: error: template-id 'what<>' for 'std::string foo::what()' does not match any template declaration
这里是一个键盘显示s错误:http://codepad.org/4HVBn9oJ
我做了什么明显的误会?或者这是不可能的与c + +模板? 将定义所有内联方法(定义模板<> foo)的工作?
再次感谢所有。
错误信息中的what <>指出这一点。请务必仔细阅读错误信息。 ;) – Xeo 2011-04-18 12:39:59
@Xeo:或者因缺乏明确性而哭泣:/ – 2011-04-18 13:02:16
接受,因为它包含了原因并且是第一个。我(不正确地)模仿专门化模板的形式。多谢你们。 – cjh 2011-04-18 23:12:36