每个范围不能超过一个Rect

时间:2014-07-03 20:52:10

标签: c++ sdl

好的,所以当我在同一个范围内制作多个rects并访问其中一个成员时, 所有其他rects(在同一范围内)变得无法访问。

如果我这样做的话;

SDL_Rect* name;
SDL_Rect* otherName;
name->x = 7;

现在尝试otherName->h = 10会在该行上崩溃

它与宣布法令的顺序无关 -

SDL_Rect* otherName;
SDL_Rect* name;
name->x = 7;

访问otherName仍然会崩溃。

1 个答案:

答案 0 :(得分:2)

在我看来,你正在声明指向SDL_Rects,但你还没有为它们分配任何内存。 你应该做点什么:

SDL_Rect* otherName = new SDL_Rect(...);
SDL_Rect* name =  new SDL_Rect(...);
name->x = 7;
otherName->h = 10;

或者只是在堆栈上分配:

SDL_Rect otherName;
SDL_Rect name;
name.x = 7;
otherName.h = 10;