请帮我检查一下下面这段关于类的代码的问题。

来源:百度知道 编辑:UC知道 时间:2024/06/25 22:55:42
class icon
{
private:
int color;
char symbol;
int x;
int y;
public:
void setColor(int& aColor);
void setSymbol(char& aChar);
void setX(int& aX);
void setY(int& aY);
};

void icon::setColor(int& aColor)
{
color = aColor;
}

void icon::setSymbol(char& aChar)
{
symbol = aChar;
}

void icon::setX(int& aX)
{
aX=x;
}

void icon::setY(int& aY)
{
aY=1;
}
还是有问题啊!!!
编译器说int icon::x is private...

void icon::setX(int& aX)
{
aX=x;
}

void icon::setY(int& aY)
{
aY=1;
}

这两个函数怎么回事?既然是设定坐标值,那就应该是给坐标赋值
应该是
void icon::setX(int& aX)
{
x=aX;
}

void icon::setY(int& aY)
{
y=aY;
}

首先确定出错的行是哪一行。你给出的这些代码中应该不会出现这个错误,应该是你在其他代码中使用了变量x,但x是private的,外部不能访问。其他变量比如color和symbol都没出现这个问题,那x应该也不会出现这个问题。
如果真的是这些代码中(改正以后)出问题,那试试把类声明和类中函数的定义(也即你给出的这些代码)放在同一个文件中,看能不能解决。
如果都不是,那我就没办法了,还是好好检查一下其他的代码吧。