vc编译出错:error C2248: 'x' : cannot access private member declared in class 'Point'解决方法
vc编译出错:error C2248: 'x' : cannot access private member declared in class 'Point'
//=====================================
// f0807.cpp
// member & non-member operator
//=====================================
#include<iostream>
using namespace std;
//-------------------------------------
class Point{
int x, y;
public:
void set(int a, int b){ x=a, y=b; }
Point operator+(const Point& d){
Point s;
s.set(x+d.x, y+d.y);
return s;
}//----------------------------------
friend ostream& operator<<(ostream& o, const Point& d);
};//===================================
inline ostream& operator<<(ostream& o, const Point& d){
return o<<"("<<d.x<<","<<d.y<<")\n";
}//====================================
int main(){
Point s,t;
s.set(2,5);
t.set(3,1);
cout<<s+t;
}//====================================
用VC编译的时候出现以下错误:
ompiling...
main.cpp
F:\C++ project\main\main.cpp(20) : error C2248: 'x' : cannot access private member declared in class 'Point'
F:\C++ project\main\main.cpp(9) : see declaration of 'x'
F:\C++ project\main\main.cpp(20) : error C2248: 'y' : cannot access private member declared in class 'Point'
F:\C++ project\main\main.cpp(9) : see declaration of 'y'
F:\C++ project\main\main.cpp(26) : error C2593: 'operator <<' is ambiguous
F:\C++ project\main\main.cpp(27) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
main.exe - 3 error(s), 1 warning(s)
我在书上看过,用friend建立的函数是可以访问局部变量的,但是这里怎么不行呢?
------解决方案--------------------
friend当然可以访问似有成员,
其意义就在于此。
不要误导lz
这里应该先声明这个函数
//=====================================
// f0807.cpp
// member & non-member operator
//=====================================
#include<iostream>
using namespace std;
//-------------------------------------
class Point{
int x, y;
public:
void set(int a, int b){ x=a, y=b; }
Point operator+(const Point& d){
Point s;
s.set(x+d.x, y+d.y);
return s;
}//----------------------------------
friend ostream& operator<<(ostream& o, const Point& d);
};//===================================
inline ostream& operator<<(ostream& o, const Point& d){
return o<<"("<<d.x<<","<<d.y<<")\n";
}//====================================
int main(){
Point s,t;
s.set(2,5);
t.set(3,1);
cout<<s+t;
}//====================================
用VC编译的时候出现以下错误:
ompiling...
main.cpp
F:\C++ project\main\main.cpp(20) : error C2248: 'x' : cannot access private member declared in class 'Point'
F:\C++ project\main\main.cpp(9) : see declaration of 'x'
F:\C++ project\main\main.cpp(20) : error C2248: 'y' : cannot access private member declared in class 'Point'
F:\C++ project\main\main.cpp(9) : see declaration of 'y'
F:\C++ project\main\main.cpp(26) : error C2593: 'operator <<' is ambiguous
F:\C++ project\main\main.cpp(27) : warning C4508: 'main' : function should return a value; 'void' return type assumed
Error executing cl.exe.
main.exe - 3 error(s), 1 warning(s)
我在书上看过,用friend建立的函数是可以访问局部变量的,但是这里怎么不行呢?
------解决方案--------------------
friend当然可以访问似有成员,
其意义就在于此。
不要误导lz
这里应该先声明这个函数