次のコードは正常に動作します。定義と用途を持つ単純なテンプレート クラスです。
#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";
}
g++ からエラーが発生します
19 行目: エラー: 'std::string foo::what()' のテンプレート ID 'what<>' がどのテンプレート宣言とも一致しません
エラーを示すコードパッドは次のとおりです。http://codepad.org/4HVBn9oJ
私が犯している明らかな間違いは何ですか?または、これは c++ テンプレートでは不可能ですか? すべてのメソッドをインラインで定義すると (template<> foo の定義を使用して) 機能しますか?
ありがとうございました。