谭浩强c++程序设计第二版之错误
#include "stdafx.h" #include <iostream> using namespace std;
class complex { public: complex(){real=0;imag=0;} complex(double r){real=r;imag=0;}; complex(double r,double i){real=r;imag=i;} complex operator+(complex&c2); void display(); private: double real; double imag; };
complex complex:: operator+(complex&c2) { return complex(real+c2.real,imag+c2.imag); }
void complex::display() { cout<<"("<<real<<","<<imag<<"i)"<<endl; }
int main() { complex c1(3,4),c2(2.5),c3; c3=c1+2.5; //原书如此,遍译无法通过,改为显示调用可以通过,如下。 //c3=c1+complex(2.5); c3.display(); return 0; }