为什么都是错? (操作符重载)小的不才,大虾们帮忙看看
//Complex.h
class Complex
{
public:
Complex();
Complex(float n1,float n2);
Complex operator+ (const Complex &S)const;
Complex operator- (const Complex &S)const;
Complex operator* (float n)const;
Complex operator~ ();
friend Complex operator * (float n,const Complex &S2);
friend Complex operator * (const Complex &S1,const Complex &S2);
friend Complex operator / (const Complex &S1,const Complex &S2);
friend Complex operator >> (istream &is,const Complex &S);
friend Complex & operator << (ostream &os,const Complex &S);
private:
float real;
float imaginary;
};
//Complex.cpp
#include<iostream.h>
#include<math.h>
#include"Complex.h"
Complex::Complex()
{real=imaginary=0.0;}
Complex::Complex(float n1,float n2)
{real=n1;
imaginary=n2;
}
Complex Complex::operator +(const Complex &S)const
{return Complex(real+S.real,imaginary+S.imaginary);
}
Complex Complex::operator -(const Complex &S)const
{return Complex(real-S.real,imaginary-S.imaginary);
}
Complex Complex::operator *(float n)const
{return Complex(real*n,imaginary*n);
}
Complex Complex::operator~ ()
{
return Complex(real,-imaginary);
}
//friend
Complex operator *(float n,Complex &S)
{
return S*n;
}
Complex operator *(Complex &S1,Complex &S2)
{
float n1=S1.real*S2.real-S1.imaginary*S2.imaginary;
float n2=S1.real*S2.imaginary+S1.imaginary*S2.real;
Complex result(n1,n2);
return result;
}
Complex operator /(Complex &S1,Complex &S2)
{float n1=(S1.real*S2.real+S1.imaginary*S2.imaginary)/(S2.real*S2.real+S2.imaginary*S2.imaginary);
float n2=(-S1.real*S2.imaginary+S1.imaginary*S2.real)/(S2.real*S2.real+S2.imaginary*S2.imaginary);
Complex result(n1,n2);
return result;
}
istream operator >> (istream & is,const Complex & S)
{
cout<<"real: ";
is>>S.real;
cout<<endl;
cout<"imaginary: ";
is>>S.imaginary;
cout<<endl;
}
ostream & operator << (ostream & os,const Complex & S)
{
os <<"( "<<S.real<<" ,"<<S.imaginary<<"i"<<" )" ;
return os;
}
//user.cpp
#include<iostream.h>
//using namespace std;
#include "Complex.h"
int main()
{
Complex a(3.0,4.0);
Complex c;
cout<<"Enter a complex number(q to quit):\n";
while(cin>>c)
{
cout <<" c is "<< c<<endl;
cout <<"complex conjugate is"<< ~c<<endl;
cout <<"a is"<< a<< endl;
cout <<"a+c is"<< a+c<< endl;
cout <<"a-c is"<< a-c<< endl;
cout <<"a*c is"<< a*c<< endl;
cout <<"a*2 is"<< a*2<< endl;
cout <<"2*c is"<< 2*c<< endl;
cout <<"a/c is"<< a/c<< endl;
cout <<"Enter a complex number(q to quit):\n";
}
cout <<"Done!\n";
return 0;
}
错误提示只是考了一部分:
e:\zm\mycomplex\complex.cpp(43) : error C2248: 'real' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(16) : see declaration of 'real'
e:\zm\mycomplex\complex.cpp(43) : error C2248: 'real' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(16) : see declaration of 'real'
e:\zm\mycomplex\complex.cpp(43) : error C2248: 'real' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(16) : see declaration of 'real'
e:\zm\mycomplex\complex.cpp(43) : error C2248: 'imaginary' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(17) : see declaration of 'imaginary'
e:\zm\mycomplex\complex.cpp(43) : error C2248: 'imaginary' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(17) : see declaration of 'imaginary'
e:\zm\mycomplex\complex.cpp(44) : error C2248: 'imaginary' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(17) : see declaration of 'imaginary'
e:\zm\mycomplex\complex.cpp(44) : error C2248: 'real' : cannot access private member declared in class 'Complex'
e:\zm\mycomplex\complex.h(16) : see declaration of 'real'