有模板的运算符重载operator<< 该如何调用?
考虑这个例子
template <unsigned dim>
class R_Tree{
};
template <unsigned dim>
ostream& operator < <(ostream &o,R_Tree <dim> &rt)
{
rt.print(o);
return o;
}
[解决办法]
你到底想问啥?
R_Tree <int> x;
cout < < x;即可。
[解决办法]
#include <iostream>
using namespace std;
template <typename T>
class R_Tree
{
public:
R_Tree(T a) : a_(a) {}
ostream& print(ostream& o)
{
o < < a_;
return o;
}
private:
T a_;
};
template <typename T>
ostream& operator < <(ostream &o, const R_Tree <T> &rt)
{
return rt.print(o);
}
int main()
{
R_Tree <int> val(10);
cout < < val < < endl;
return getchar();
}
[解决办法]
VC6對墨板的支持不好,用VC2003, VC2005,MinGW, DevCpp……
[解决办法]
把HappyTree(笨笨·天行健)程序
template <typename T>
ostream& operator < <(ostream &o, R_Tree <T> &rt)
{
return rt.print(o);
}
去掉const
就可以正确运行