【发布时间】:2018-07-19 17:43:44
【问题描述】:
所以我在获取使用 SFML 形状的指针时遇到了一些麻烦。我不确定这是否与 SFML 有关,或者我做错了什么。
在 Draw() x(a ControlWindow) 中不包含有效值,它只显示“???”,如下所示。但是 m_controls(map) 包含控件对象的正确值。
我对 C++ 很陌生,因此非常感谢任何帮助。
异常
Exception thrown at 0x60B26EE5 (sfml-graphics-2.dll) in OokiiUI.exe: 0xC0000005: Access violation reading location 0x00000000.
主要
vector<WindowControl> windowControls;
void Draw ();
int main ()
{
RectangleShape rect(Vector2f(120,120));
WindowControl windowControl(nullptr,0);
Control testControl(&windowControl,1);
testControl.SetShape(&rect);
windowControl.AddControl(testControl);
windowControls.push_back(windowControl);
return 0;
}
窗口控件
class WindowControl : Control
{
public:
WindowControl ( WindowControl * windowControl, uint64_t uint64 )
: Control ( windowControl, uint64 )
{
}
void AddControl(Control control)
{
m_controls.insert_or_assign(control.GetId(), control);
m_controlPtrs.push_back(&control);
}
vector<Control*>* GetControls()
{
return &m_controlPtrs;
}
private:
map<uint64_t, Control> m_controls;
vector<Control*> m_controlPtrs;
};
画
for (auto x : windowControls)
{
vector<Control*> *controlPtrs = x.GetControls();
window->draw(x.GetControl(0)->GetShape());
}
【问题讨论】:
-
这可能是由于您复制对象而不是将该指针添加到向量造成的。该本地对象超出范围,并且您的指针错误:仅供参考
m_controlPtrs.push_back(&control);是错误的