#include<iostream>
using namespace std;
class A{
int x;
public:
A(int i=0):x(i){}
const A operator+(const A& lhs) const
{
return A(x+lhs.x);
}
friend ostream& operator<<(ostream&,const A&);
};
ostream& operator<<(ostream& os,const A& a)
{
os<<a.x;
return os;
}
int main()
{
A a(3);
A b,c;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
c=a+b;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
}
--------------------Configuration: 12_6 - Win32 Debug--------------------
Compiling...
12_6.cpp
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(16) : error C2248: 'x' : cannot access private member declared in class 'A'
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(5) : see declaration of 'x'
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(23) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(24) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(25) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(27) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(28) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(29) : error C2593: 'operator <<' is ambiguous
C:\Documents and Settings\Owner\My Documents\Thinking_in_C++作业\12_6.cpp(30) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.
12_6.exe - 1 error(s), 0 warning(s)