我有一个带有CGrect变量的c ++类,我在尝试访问它时遇到了段错误。
class Parent
{
//with some virtual functions/dtors
};
class Child
{
public:
void SetRect(CGRect rect) { mRect = rect; }
CGRect GetRect() { return mRect; }
int GetIndex() { return mIndex; }
private:
CGRect mRect;
int mIndex;
};
我正在做
CGRect rect = childPtr->GetRect();
来自对象c代码并且它是段错误 我在调用之前打印了* childPtr,并且rect看起来没有预期的数据值。
int index = childPtr->GetIndex();
来自相同的对象c代码(* .mm),但工作正常。
知道我为什么会遇到段错误吗? 谢谢
这与虚拟功能有关。
(gdb)p singlePuzzlePiece-> GetRect()
程序收到信号EXC_BAD_ACCESS,无法访问内存。 原因:地址为0x00000001的KERN_PROTECTION_FAILURE 0x00000001在? () 无法访问地址0x1处的内存 正在调试的程序在从GDB调用的函数中发出信号。 GDB保留在接收信号的帧中。 要更改此行为,请使用“set unwindonsignal on” 将放弃对包含该函数的表达式(在0x1处)的评估。 (gdb)
不知何故,该函数未正确编译?
答案 0 :(得分:0)
在目标C中获取rect变量 你这样做
int indexX = rect.origin.x
int indexY = rect.origin.y
等。