一个简单的程序,但是构造函数报错

来源:百度知道 编辑:UC知道 时间:2024/07/01 07:13:39
#include <iostream.h>

class Location
{
public:
Location(int x = 0, int y = 0);
void Move(int x=5, int y=5);
void ValueX(int);
int ValueX();
void ValueY(int);
int ValueY();
private:
void Set(int x, int y);
int X, Y;
}

Location::Location(int x, int y)
{
Set(x, y);
}

void Location::Move(int x, int y)
{
Set(x, y);
}

void Location::ValueX(int val)
{
X=val;
}

void Location::ValueY(int val)
{
Y=val;
}

int Location::ValueX()
{
return X;
}

int Location::ValueY()
{
return Y;
}

void Location::Set(int x, int y)
{
X=x;
Y=y;
}

int main(int argc, char ** argv)
{
Location A, B;
A.Move();
A.ValueX(15);
cout << A.ValueX() << A.ValueY() << endl;
B.Move(9, 19);

Location::Location(int x, int y)
{
//Set(x, y); //还没有构造,你就使用了里面的函数???
X=x; Y=y;
}

把Set(int , int),放到public区域就好了。通过它访问私有成员,但是你把它也放到private了,试试看。