C++ 为什么我在读回私有类变量时会获得EXC_BAD_访问权限?
我有一个C++ 为什么我在读回私有类变量时会获得EXC_BAD_访问权限?,c++,class,runtime-error,exc-bad-access,C++,Class,Runtime Error,Exc Bad Access,我有一个矩形类,如下所示: 标题: class Rectangle: public Polygon { private: float _width, _height; public: Rectangle(float width, float height); float getWidth(float* width) const; float getHeight(float* height) const; bool isCollidingWith(Recta
矩形
类,如下所示:
标题:
class Rectangle: public Polygon {
private:
float _width, _height;
public:
Rectangle(float width, float height);
float getWidth(float* width) const;
float getHeight(float* height) const;
bool isCollidingWith(Rectangle* other) const;
};
选定的实施:
Rectangle::Rectangle(float width, float height) : Polygon(explodeRect(width, height, new struct vertex[4]), 4) {
printf("creating rect %f x %f\n", width, height);
_width = width;
_height = height;
printf("set _width to %f\n", _width);
}
float Rectangle::getWidth(float* width) const {
printf("_w: %f\n", _width);
*width = _width;
return *width;
//return (*width = _width);
}
float Rectangle::getHeight(float* height) const {
return (*height = _height);
}
我初始化了矩形
类的一个实例,输出表明\u width
变量被正确分配。但是,当我稍后尝试使用getWidth
方法读取变量时,我在以下行中得到EXC\u BAD\u ACCESS
错误:
printf("_w: %f\n", _width);
为什么我不能再读取此变量?我对\u height
变量也有同样的问题
编辑:我还想指出,如果我跳过读取宽度,我会在尝试直接从对象读取公共变量时出错,例如,当我尝试使用obj->x
读取其x位置时
编辑2:这可能是因为对象是Rectangle
的子类的实例,并且该子类是在与Rectangle
不同的文件中定义的吗?我还从第三个文件中读取值
编辑3:下面有更多代码
我正在尝试用OpenGL重新创建俄罗斯方块。在我的display
方法中,我有以下代码来绘制矩形:
if(fallingBlock != nullptr) {
printf("drawing falling block at (%f, %f)\n", fallingBlock->x, fallingBlock->y);
draw(fallingBlock);
}
fallingBlock
定义为文件顶部的全局变量:
Block* fallingBlock;
从我的main
调用initVars
方法,该方法随后调用startDroppingBlock
方法。这是:
void startDroppingBlock() {
Block* block = availableBlocks[random() % numAvailableBlocks].copy();
block->x = 0.5;
block->y = SCREEN_TOP;
block->dy = -0.01f;
//printf("copied block is at (%f, %f)\n", block->x, block->y);
fallingBlock = block;
}
下面是我的块绘制方法:
void draw(Block* obj) {
bool shape[3][3];
obj->getShape(shape);
//printf("got shape: {%d, %d, %d}, {%d, %d, %d}, {%d, %d, %d}\n", shape[0][0], shape[0][1], shape[0][2], shape[1][0], shape[1][1], shape[1][2], shape[2][0], shape[2][1], shape[2][2]);
/*float pieceWidth;
obj->getWidth(&pieceWidth);
pieceWidth /= 3.0f;*/
float pieceWidth = obj->getWidth();
for(unsigned int i=0; i<3; i++) {
for(unsigned int j=0; j<3; j++) {
if(shape[i][j]) {
Square rect = Square(pieceWidth);
rect.x = obj->x + pieceWidth * j;
rect.y = obj->y + pieceWidth * i;
rect.color = obj->color;
draw(&rect);
}
}
}
}
void draw(块*obj){
布尔形[3][3];
obj->getShape(形状);
//printf(“获得形状:{%d,%d,%d},{%d,%d,%d},{%d,%d,%d}\n”,形状[0][0],形状[0][1],形状[0][2],形状[1][0],形状[1][1],形状[1][2],形状[2][0],形状[2][1],形状[2][2]);
/*浮动件宽度;
obj->getWidth(&pieceWidth);
工件宽度/=3.0f*/
float-pieceWidth=obj->getWidth();
对于(无符号整数i=0;iy+pieceWidth*i;
rect.color=obj->color;
绘制(&rect);
}
}
}
}
我在第[…]行遇到EXC_BAD_访问错误。为什么我不能再读取此变量?我也遇到了与_height变量相同的问题。[稍后…]我尝试了float-pieceWidth;obj->getWidth(&pieceWidth);
和obj->getWidth(new float)
-在我使用传入指针之前,实际错误在我读取\u width
的行上。[稍后…]我修改了getWidth和getHeight方法,只返回_width和_height。现在我只返回_width时出错
在这种情况下,我看到您使用的是一个矩形*
指针作为obj->getWidth
,如果obj
不是有效的指针,这也会导致严重的访问错误
需要注意的是,我根本不太了解您的getter方法。它的简化(可能是标准)版本可能是:
float Rectangle::getWidth() const {
return _width;
}
唯一不同的是,使用时:
// float a;
// float b;
a = rect.getWidth(&b);
您现在可以执行以下操作:
// float a;
// float b;
a = b = rect.getWidth();
这可能更干净,并且肯定不会导致这样的错误。一个好的经验法则是在可能的情况下永远不要使用指针。如果您需要修改函数中的变量,只需使用引用。调用
GetWidth
的代码在哪里?为什么getter接受参数并返回值?这是多余的…@matstpeterson我正试图用OpenGL重新制作俄罗斯方块,所以我有一个display
方法调用draw
方法,该方法使用矩形的宽度来绘制矩形。@Borgeader我被告知在getter方法中创建变量有可能导致分段错误。我仍然让它返回值,以便我可以使用内联方法。@Greg返回一个指针或对局部变量的引用是一个问题,但这不是你在这里要做的。好的,我修改了getWidth
和getHeight
方法,只返回\u width
和\u height
。现在我只在返回width;
@Greg上得到一个错误,它是表示您的obj
不是指向Rectangle*
的有效指针。如果您向我显示从声明obj
到调用obj->getWidth
的代码,我可能会找出问题出在哪里。@Greg,那么Rectangle
是Block
?矩形*
对象在哪里?Block
继承自正方形
,它继承自矩形
,它继承自多边形
。