由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 【c++里override输出<<总出错】
相关主题
C++ Q22: ostream请教一个写程序的问题
这个C++程序的运行结果是什么1 11 21 1211 sequence的代码
问个C++模板定义的问题一道google电面题,估计挂了。。。
c++ class definition一个N个数的int数组如何找到3个majority的数?
Tree Iterator && operator overloading的一个问题C++ Q60 calling virtual function in constructor (JPMorgan)
请教个G题目弱问一个c++编程题
问个关于c++ auto_pter的问题为什么我在array里用IsOdd 总出错?
C: what is the output?【为什么我写的reverse string总出错】
相关话题的讨论汇总
话题: treenode话题: ostream话题: template话题: stream话题: class
进入JobHunting版参与讨论
1 (共1页)
h*****g
发帖数: 944
1
谢谢大家帮我看看,我实在不知道为啥错了
#include
using namespace std;
template< class T>class TreeNode{
public:
T data;
TreeNode(T v);
TreeNode *left;
TreeNode *right;
friend ostream &operator<<(ostream &stream, TreeNode &o);
};
template TreeNode ::TreeNode(T v)
:data(v), left(NULL), right(NULL)
{
}
template ostream &operator<<(ostream &stream, TreeNode &o){
stream<data<<" ";
}
int main(){
TreeNode * head = new TreeNode (2);
cout< }
r*******y
发帖数: 1081
2
illegal conversion from pointer to reference.

【在 h*****g 的大作中提到】
: 谢谢大家帮我看看,我实在不知道为啥错了
: #include
: using namespace std;
: template< class T>class TreeNode{
: public:
: T data;
: TreeNode(T v);
: TreeNode *left;
: TreeNode *right;
: friend ostream &operator<<(ostream &stream, TreeNode &o);

s*****n
发帖数: 231
3
template ostream &operator<<(ostream &stream, TreeNode &o){
stream<data<<" ";
//add your return value here!
return stream;
}
s*****n
发帖数: 231
4
And yes, as romancity pointed out:
stream<data<<" ";
should be
stream<
h*****g
发帖数: 944
5
谢谢啊
好像我pass到那个override function的变量是个pointer, 所以要o->data吧
不然的话是不是要写成一下的样子?
template ostream &operator<<(ostream &stream, TreeNode *&o){

【在 s*****n 的大作中提到】
: And yes, as romancity pointed out:
: stream<data<<" ";
: should be
: stream<
h*****g
发帖数: 944
6
请问正确的该怎么写啊?

【在 r*******y 的大作中提到】
: illegal conversion from pointer to reference.
c****o
发帖数: 1280
7
the operator is also a template operator, you need to first claim it as a
template operator, like
template
class TreeNode;
template
ostream & operator <<(ostream & stream, TreeNode & O)
template
class TreeNode{
...
friend ostream &operator<< <>(ostream &stream, TreeNode &o);
..
};
template
then implement the operator

【在 h*****g 的大作中提到】
: 谢谢大家帮我看看,我实在不知道为啥错了
: #include
: using namespace std;
: template< class T>class TreeNode{
: public:
: T data;
: TreeNode(T v);
: TreeNode *left;
: TreeNode *right;
: friend ostream &operator<<(ostream &stream, TreeNode &o);

1 (共1页)
进入JobHunting版参与讨论
相关主题
【为什么我写的reverse string总出错】Tree Iterator && operator overloading的一个问题
C++ Q80: What is the output of the following code?请教个G题目
C++ Q96: function inheritance问个关于c++ auto_pter的问题
请教C/C++小C: what is the output?
C++ Q22: ostream请教一个写程序的问题
这个C++程序的运行结果是什么1 11 21 1211 sequence的代码
问个C++模板定义的问题一道google电面题,估计挂了。。。
c++ class definition一个N个数的int数组如何找到3个majority的数?
相关话题的讨论汇总
话题: treenode话题: ostream话题: template话题: stream话题: class