私は今stackoverflowで私の問題に関する質問のカップルを読んで、どれも私の問題を解決するようだ。または私は多分それを間違ってしまったかもしれません... インライン関数にすると、<<
がオーバーロードされます。しかし私はそれを私の場合には機能させる方法はありますか?友人演算子<<テンプレートクラスのオーバーロード
warning: friend declaration std::ostream& operator<<(std::ostream&, const D<classT>&)' declares a non-template function
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
/tmp/cc6VTWdv.o:uppgift4.cc:(.text+0x180): undefined reference to operator<<(std::basic_ostream<char, std::char_traits<char> >&, D<int> const&)' collect2: ld returned 1 exit status
template <class T>
T my_max(T a, T b)
{
if(a > b)
return a;
else
return b;
}
template <class classT>
class D
{
public:
D(classT in)
: d(in) {};
bool operator>(const D& rhs) const;
classT operator=(const D<classT>& rhs);
friend ostream& operator<< (ostream & os, const D<classT>& rhs);
private:
classT d;
};
int main()
{
int i1 = 1;
int i2 = 2;
D<int> d1(i1);
D<int> d2(i2);
cout << my_max(d1,d2) << endl;
return 0;
}
template <class classT>
ostream& operator<<(ostream &os, const D<classT>& rhs)
{
os << rhs.d;
return os;
}
有益であり、これについての最近の質問がありました/質問/ 4571611 /バーチャルオペレータ/ –
@ダニエル - 私はテンプレートクラスのオーバーロード時に問題が発生しません – starcorn
私はあなたが与えられた答えで質問を変更しない方が良いと思います。元の問題が何であるかを判断するのが難しくなります。最後に** EDIT **を追加して、問題を解決した変更を追加したいかもしれませんが、質問が残業に変わると混乱することがあります。最初の場所。 –