c++模板类的友元重载如何写

来源:百度知道 编辑:UC知道 时间:2024/06/05 19:10:04
template<class Entry>
istream& operator>><Entry>(istream& in,Binary_tree<Entry>& subTree) {
//重载操作: 输入并建立一棵二叉树,in是输入流对象。
subTree.CreateBinTree(in); //建立二叉树
return in;
};
编译出错:
'operator >>' : illegal use of explicit template arguments
我所用的编译环境是visual stdio 2005,这题在VC++ 6.0中可以编译通过,但visual stdio2005不可以。

istream& operator>><Entry>(istream& in,Binary_tree<Entry>& subTree)

=====》

friend istream& operator>>(istream& in,Binary_tree<Entry>& subTree)

给你个例子:

#include <iostream>
#include <vector>
using namespace std;
template <class Entry>
class tree
{
vector<Entry> v;

friend istream& operator>> (istream& in, vector<Entry>& v)
{
Entry t;
cin >> t;
v.push_bach(t);
return in;
}
};

int main()
{
tree<int> t;
return 0;
}