64,654
社区成员
发帖
与我相关
我的任务
分享
////////////////////////////////////////
#include<iostream>
using namespace std;
class test
{
private:
int x,y;
public:
test(int i,int j){
x = i;
y = j;
}
~test(){}
friend ostream& operator<<(ostream& stream,test ob);
};
ostream& operator<<(ostream& stream,test ob){
stream<<ob.x<<","<<ob.y<<endl;
return stream;
}
int main()
{
test a(5,6),b(20,23);
cout<<a<<endl<<b<<endl;
return 0;
}
///////////////////////////////
错误为:
ostream.cpp(24) : error C2248: 'x' : cannot access private member declared in class 'test'
ostream.cpp(13) : see declaration of 'x'
ostream.cpp(24) : error C2248: 'y' : cannot access private member declared in class 'test'
ostream.cpp(13) : see declaration of 'y'
ostream.cpp(31) : error C2593: 'operator <<' is ambiguous
#include<iostream>
using namespace std;
class test
{
private:
int x,y;
public:
test(int i,int j){
x = i;
y = j;
}
~test(){}
void print(ostream& stream);
};
void test::print(ostream& stream)
{
stream<<x<<","<<y<<endl;
};
ostream& operator<<(ostream& stream,test ob){
ob.print(stream);
return stream;
}
int main()
{
test a(5,6),b(20,23);
cout<<a<<endl<<b<<endl;
return 0;
}