本文介绍了通常指针导致分段错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的班级ComponentArea如下所示:
My class "ComponentArea" looks like this:
#ifndef COMPONENTAREA_H
#define COMPONENTAREA_H
#include <QObject>
#include <QWidget>
#include <QScrollArea>
#include <QtDesigner/QtDesigner>
#include <QMouseEvent>
#include <QPainter>
#include <Components/Sockets/socket.h>
class Socket;
class ComponentArea : public QScrollArea
{
Q_OBJECT
public:
ComponentArea(QWidget* parent);
void connectSockets(Socket* a, Socket* b);
void childBlock_childSocket_mousePressEvent(Socket* sender, QMouseEvent* event);
void childBlock_childSocket_mouseReleaseEvent(Socket* sender, QMouseEvent* event);
private:
Socket* pressedSocket;
void mouseReleaseEvent(QMouseEvent *event);
};
#endif // COMPONENTAREA_H
在构造函数中,我将pressedSocket设置为0:
In the constructor, I set pressedSocket to 0:
pressedSocket = 0;
每当我尝试使用pressedSocket时,在其中一个事件中,我会遇到分段错误:
Whenever I try to use pressedSocket, in one of the events, I get a Segmentation Fault:
void ComponentArea::childBlock_childSocket_mousePressEvent(Socket *sender, QMouseEvent *event)
{
if((pressedSocket == 0) && (event->button() == Qt::LeftButton)) //SIGSEGV here
{
pressedSocket = sender;
}
}
当我尝试在此行检查pressedSocket时,调试器告诉我pressedSocket< ;没有这样的价值>。
我看不出我做错了什么。有什么建议吗?在此先感谢!
When I try to inspect pressedSocket at this line, the Debugger tells me "pressedSocket <no such value>".
I don't see what I am doing wrong. Any advices? Thanks in advance!
推荐答案
这篇关于通常指针导致分段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!