关于重载运算符<<的问题

Gzd2003 2008-11-13 04:26:45
存在以下程序,我想针对复数Complex类重载运算符<<,但是编译出错,该如何编写重载运算符<<的方法呢?谢谢。
#include <iostream.h>
class Complex
{
private:
int i;
int j;
public:
Complex(int i=0,int j=0)
{
this->i=i;
this->j=j;
}
ostream operator<<(Complex c)
{
ostream o;
o<<c.i<<"+"<<c.j<<"i"<<endl;
return o;
}
};

void main()
{
Complex c(2,3);
cout<<c;
}
编译错误信息如下:
--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
CopyTest.cpp
d:\Test\CopyTest.cpp(15) : error C2248: 'ostream::ostream' : cannot access protected member declared in class 'ostream'
d:\program files\microsoft visual studio\vc98\include\ostream.h(99) : see declaration of 'ostream::ostream'
d:\Test\CopyTest.cpp(17) : error C2248: 'ostream::ostream' : cannot access protected member declared in class 'ostream'
d:\program files\microsoft visual studio\vc98\include\ostream.h(100) : see declaration of 'ostream::ostream'
d:\Test\CopyTest.cpp(24) : error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class Complex' (or there is no acceptable conversion)
Error executing cl.exe.

Test.exe - 3 error(s), 0 warning(s)
...全文
296 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
lann64 2008-11-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 Gzd2003 的回复:]
一定要使用友元函数吗?我友元函数也会,但是我想知道能够写成类成员函数?谢谢。
[/Quote]
对于重载 “<<”和“>>”操作符,只能是友元函数,不能是成员函数。
道理也很简单,成员函数的第一个形参是隐含的this指针,而在重载的操作符里,第一参数必须是“流 ”
liumingrong 2008-11-14
  • 打赏
  • 举报
回复
不可以写成成员函数,
因为cout << A;
要么调用函数operator<<(cout,A)
要么调用cout的成员函数cout.operator<<(A) //不行
不会调用A的成员函数
[Quote=引用 7 楼 Gzd2003 的回复:]
一定要使用友元函数吗?我友元函数也会,但是我想知道能够写成类成员函数?谢谢。
[/Quote]
就呆在云上 2008-11-14
  • 打赏
  • 举报
回复
很简单的问题了,你这个东西声明为友元就是了如下:
#include "iostream" 
using namespace std;

class Complex
{
private:
int i;
int j;
public:
Complex(int i=0,int j=0) {
this->i=i;
this->j=j;
}
friend ostream &operator <<(ostream &o, Complex c) {

o <<c.i <<"+" <<c.j <<"i" <<endl;
return o;
}
};

void main()
{
Complex c(2,3);
cout <<c;
}


输出2+3i
Gzd2003 2008-11-14
  • 打赏
  • 举报
回复
ding
nsh_nsh 2008-11-14
  • 打赏
  • 举报
回复
在连续输出的时候是不能写成内的成员函数的
Gzd2003 2008-11-13
  • 打赏
  • 举报
回复
一定要使用友元函数吗?我友元函数也会,但是我想知道能够写成类成员函数?谢谢。
小人物- 2008-11-13
  • 打赏
  • 举报
回复
少“&”
SearchLife 2008-11-13
  • 打赏
  • 举报
回复
#include <iostream.h>
class Complex
{
private:
int i;
int j;

public:
Complex(int i=0,int j=0)
{
this->i=i;
this->j=j;
}
friend ostream& operator<<(ostream& os,const Complex& c)
{
os << c.i << "+" <<c.j <<"i" <<endl;
return os;
}
};

void main()
{
Complex c(2,3);
cout << c;
}
jia_xiaoxin 2008-11-13
  • 打赏
  • 举报
回复
需要使用友元函数

#include <iostream.h> 

class Complex
{
private:
int i;
int j;
public:
Complex(int i=0,int j=0)
{
this->i=i;
this->j=j;
}
friend ostream & operator << ( ostream & o, const Complex & c);
};

ostream & operator << ( ostream & o, const Complex & c)
{
o << c.i << "+" << c.j << "i" << endl;
return o;
}

void main()
{
Complex c(2,3);
cout << c;
}
zzq0406 2008-11-13
  • 打赏
  • 举报
回复
friend ostream &operator <<(ostream &o,Complex c)
{
//ostream o;
o <<c.i <<"+" <<c.j <<"i" <<endl;
return o;
}
这样应该能实现你的 要求把。
nsh_nsh 2008-11-13
  • 打赏
  • 举报
回复
#include <iostream.h>
class Complex
{
public:
int i;
int j;
public:
Complex(int i=0,int j=0)
{
this->i=i;
this->j=j;
}

};
ostream & operator < <(ostream &os,Complex c)
{
//ostream o;
os < <c.i < <"+" < <c.j < <"i" < <endl;
return os;
}
void main()
{
Complex c(2,3);
cout < <c;
}
关于LZ的,我想说以下几点:
第一: < <操作符重载是不能作为内的成员函数存在的,因为流的左边是cout这类的操作符,而不是内的一种对象
第二:你把内的变量定义为了private的,不能访问了,当然在你让成员函数去访问是没有错误的
第三:操作符 < <重载时需要传入两个参数
以上程序是我修改了哈的,在vc60.0下编译通过
nsh_nsh 2008-11-13
  • 打赏
  • 举报
回复
#include <iostream.h>
class Complex
{
public:
int i;
int j;
public:
Complex(int i=0,int j=0)
{
this->i=i;
this->j=j;
}

};
ostream & operator<<(ostream &os,Complex c)
{
//ostream o;
os<<c.i<<"+"<<c.j<<"i"<<endl;
return os;
}
void main()
{
Complex c(2,3);
cout<<c;
}
关于LZ的,我想说以下几点:
第一:<<操作符重载是不能作为内的成员函数存在的,因为流的左边是cout这类的操作符,而不是内的一种对象
第二:你把内的变量定义为了private的,不能访问了,当然在你让成员函数去访问是没有错误的
第三:操作符<<重载时需要传入两个参数
以上程序是我修改了哈的,在vc60.0下编译通过

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧