是在是不想发到这的,但是考虑到又很多任可能会的就.........而且c++ 论坛很久都没有人给与答案不的不求助
人气旺盛的c板块了
//main函数
#include <iostream>
using namespace std;
#include "complex.h"
int main(void)
{
Complex a( 3.0, 4.0 );
Complex c( 8.7, 9.0 );
Complex cd;
cd = a.operator +(c);
cout << cd;
/* 在这个地方出错.d:\net\djx\Main.cpp(12): error C2679: 二 进制“<<” : 没有找到接受“<未知>”类型的右操作数的运算符(或没有可接受的转换)*/
cout << "Bye!";
return 0;
}
//头文件
#ifndef COMPLEX_H_
#define COMPLEX_H_
using namespace std;
class Complex
{
private:
double real;
double imaginary;
public:
Complex();
Complex( double rea , double ima = 0.0 );
void setReal( double rea );
void setImag( double ima );
Complex operator+( const Complex &a );
friend ostream & operator<<( ostream &, Complex & );
};
#endif
//函数实现
#include <iostream>
using namespace std;
#include "complex.h"
Complex::Complex()
{
real = 0.0;
imaginary = 0.0;
}
Complex::Complex( double rea , double ima )
{
real = rea;
imaginary = ima;
}
void Complex::setReal( double rea )
{
real = rea;
}
void Complex::setImag( double ima )
{
imaginary = ima;
}
Complex Complex::operator+( const Complex &a )
{
return Complex( real + a.real, imaginary + a.imaginary );
}
ostream & operator<< ( ostream &os , Complex &com )
{
os << "the real is: "<< com.real;
os << "\nthe imaginary is "<< com.imaginary ;
return os;
}