重载运算符的错误
2016-05-19 22:59
253 查看
1. 在用成员函数进行运算符重载时,只能有一个参数。否则出现错误:must take either zero or one argument;
2.错误:
2.错误:
#include<iostream> using namespace std; class Complex { public: Complex(int r, int i){real=r; ima=i;} int getR(){return real;} int getI(){return ima;} void display(){cout<<real<<" + "<<ima<<"i"<<endl<<endl;} friend Complex operator + (Complex&, Complex&); friend Complex operator - (Complex&, Complex&); private: int real; int ima; }; Complex Complex::operator + (Complex& a, Complex& b)<span style="white-space:pre"> </span>//error! { int r=a.getR()+b.getR(); int i=a.getI()+b.getI(); Complex c(r, i); return c; } Complex Complex::operator - (Complex& a, Complex& b)<span style="white-space:pre"> </span>//error! { int r=a.getR()-b.getR(); int i=a.getI()-b.getI(); Complex c(r, i); return c; } int main() { Complex a(3,5); a.display(); Complex b(7,7); b.display(); Complex c=a+b; cout<<"a+b=\n"; c.display(); Complex d=a-b; cout<<"a-b=\n"; d.display(); return 0; }错误同1. 因加了Complex类的域名变成成员函数,而非友元函数
相关文章推荐
- rsync+inotify实现数据同步
- 做 h5 推荐几个东西吧
- SQL Server 如何实现"选择所有行"和"编辑所有行"
- 窥探 kernel --- 进程调度的目标,nice值,静态优先级,动态优先级,实时优先级
- jmeter(1) 组件执行顺序
- shell简单使用(五)变量之二
- 第四百一十二天 how can I 坚持
- Windows 下的 Java JDK安装
- css中的baseline
- 如何利用sorted函数对列表,元组或者对象进行排序
- SQL学习笔记3——用代码实现数据的 增、删、改、查
- 博客同步更新说明
- Android MVP
- 数据库语言
- 第一份csdn博客
- C++字符串操作详解(整理)
- Problem S
- UltraISO制作U盘启动盘
- UIScrollView常见的使用方法
- maven依赖关系中Scope的作用