以下のコードに似た基本クラスがあります。coutで使用するために<<をオーバーロードしようとしています。ただし、g++は次のように言っています。
base.h:24: warning: friend declaration ‘std::ostream& operator<<(std::ostream&, Base<T>*)’ declares a non-template function
base.h:24: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning
クラス宣言/プロトタイプの<<の後に<>を追加してみました。しかし、私はそれを取得しdoes not match any template declaration
ます。演算子定義を完全にテンプレート化しようとしましたが(必要です)、演算子を手動でインスタンス化して、次のコードでのみ機能させることができました。
base.h
template <typename T>
class Base {
public:
friend ostream& operator << (ostream &out, Base<T> *e);
};
base.cpp
ostream& operator<< (ostream &out, Base<int> *e) {
out << e->data;
return out;
}
ヘッダーbase.hにこれまたは類似のものを入れたいだけです。
template <typename T>
class Base {
public:
friend ostream& operator << (ostream &out, Base<T> *e);
};
template <typename T>
ostream& operator<< (ostream &out, Base<T> *e) {
out << e->data;
return out;
}
プロトタイプの<<と()の間に<>を入れるとこれが修正されるはずだとオンラインで読んだことがありますが、修正されません。これを単一の関数テンプレートにまとめることはできますか?