首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

有模板的运算符重载operator<< 该怎么调用

2012-02-08 
有模板的运算符重载operator 该如何调用?考虑这个例子template unsigneddimclassR_Tree{}template u

有模板的运算符重载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
就可以正确运行

热点排行