我已经阅读了几个有关我的问题在stackoverflow现在,并没有似乎解决我的问题。或者我也许做错了... 超载<<
如果我把它变成一个内联函数。但是我怎么让它在我的情况下工作?重载朋友operator <<模板类
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;
}
有最近关于这可能是有益的一个问题:http://stackoverflow.com/questions/4571611/virtual-operator/ – 2011-01-11 16:52:00
@Daniel - 它不会占用我为模板类重载时遇到的问题 – starcorn 2011-01-11 16:55:12
我认为如果不用给定的答案修改问题会更好。这使得更难确定原来的问题是什么。你可能想在最后加上一个** EDIT **来解决这个问题,但是我发现当问题发生超时并且我不得不拉起历史来查看实际上在第一名。 – 2011-01-11 19:13:12