我得到一个错误,说我不能访问私有成员x和y。如何编写方法getX()和getY(),以便他们可以看到x和y?谢谢。用C++编写一个简单的类
#include <iostream>
#include <string>
using namespace std;
class Point {
public:
Point(int x, int y);
Point();
int getX();
int getY();
private:
int x, y;
};
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
void main() {
Point p(5,5);
Point g;
cout << p.x << endl;
cout << g.y;
string s;
cin >> s;
}
你还缺少构造函数定义 – dcousens 2011-03-24 02:54:35