我现在已经在StackOverflow.com上阅读了几个关于我的问题的问题,但似乎没有一个能解决我的问题.或者我可能做错了... <<如果我将其转换为内联函数,则重载会起作用.但是我如何让它在我的情况下工作?
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> …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写二叉树.为什么以下代码报告错误C2039,"'<<':不是'btree <T>'的成员",即使<<运算符已被声明为btree类中的友元函数?
#include<iostream>
using namespace std;
template<class T>
class btree
{
public:
friend ostream& operator<<(ostream &,T);
};
template<class T>
ostream& btree<T>::operator<<(ostream &o,T s)
{
o<<s.i<<'\t'<<s.n;
return o;
}
Run Code Online (Sandbox Code Playgroud)