关于复数的操作,编译器通过不了。
#include<iostream>using namespace std;
class A
{ friend ostream& operator<<(ostream& object,const A& B);
friend istream& operator>>(istream& object,const A& B);
private:
double real;
double image;
public:
A(double i=0,double j=0);
void set(double i,double j);
A& operator+(const A& B);
bool operator==(const A& B);
};
void A::set(double i,double j)
{
real=i;image=j;
}
ostream& operator<<(ostream& object,const A& B)
{
object<<"(";
object<<B.real;
object<<",";
object<<B.image;
object<<")";
return object;
}
istream& operator>>(istream& object,const A& B)
{
char ch;
object>>ch;
object>>B.real;
object>>ch;
object>>B.image;
object>>ch;
return object;
}
A& A::operator+(const A& B)
{
A C;
C.real=real+B.real;
C.image=image+B.image;
return C;
}
bool A::operator==(const A& B)
{
return((image=B.image)&&(real=B.real));
}
int main( )
{
A num1;
A num2;
A num3;
num1.set(23,24);
cout<<num1<<endl;
cout<<num2<<endl;
cout<<"Please input two nums"<<endl;
cin>>num2;
cout<<endl;
cout<<num2;
num3=num1+num2;
cout<<num3;
return 0;
}
Configuration: 5 - Win32 Debug--------------------
Compiling...
5.cpp
D:\C++\复数加减\5.cpp(23) : error C2248: 'real' : cannot access private member declared in class 'A'
D:\C++\复数加减\5.cpp(7) : see declaration of 'real'
D:\C++\复数加减\5.cpp(25) : error C2248: 'image' : cannot access private member declared in class 'A'
D:\C++\复数加减\5.cpp(8) : see declaration of 'image'
D:\C++\复数加减\5.cpp(33) : error C2248: 'real' : cannot access private member declared in class 'A'
D:\C++\复数加减\5.cpp(7) : see declaration of 'real'
D:\C++\复数加减\5.cpp(33) : error C2593: 'operator >>' is ambiguous
D:\C++\复数加减\5.cpp(35) : error C2248: 'image' : cannot access private member declared in class 'A'
D:\C++\复数加减\5.cpp(8) : see declaration of 'image'
D:\C++\复数加减\5.cpp(35) : error C2593: 'operator >>' is ambiguous
D:\C++\复数加减\5.cpp(44) : warning C4172: returning address of local variable or temporary
D:\C++\复数加减\5.cpp(57) : error C2593: 'operator <<' is ambiguous
D:\C++\复数加减\5.cpp(58) : error C2593: 'operator <<' is ambiguous
D:\C++\复数加减\5.cpp(60) : error C2593: 'operator >>' is ambiguous
D:\C++\复数加减\5.cpp(62) : error C2593: 'operator <<' is ambiguous
D:\C++\复数加减\5.cpp(64) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.
5.obj - 11 error(s), 1 warning(s)