我的 Xlib 包装类中的分段错误 (C++)
我正在尝试为 Xlib 库创建一个包装类,但出现分段错误!我是 C++ 新手,这可能超出了我的深度,也许我的目标设置得很高,除了这个问题之外,有人可以告诉我为什么会出现此分段错误吗?
源文件
头文件
main.cpp
我相信这是我得到的回溯:
Program received signal SIGSEGV, Segmentation fault.
In XMapWindow () (/usr/lib/libX11.so.6)
At /home/elliot/Programming/C and C++/XWindows/src/MyWindow.cpp:49
I am attempting to create a wrapper class to the Xlib library, but I get a segmentation fault! I am new to C++ and this is probably out of my depth and maybe have my goals set to high, other than that problem, can someone tell my why I'm getting this segmentation fault?
source file
header file
main.cpp
I believe this is the back trace I get:
Program received signal SIGSEGV, Segmentation fault.
In XMapWindow () (/usr/lib/libX11.so.6)
At /home/elliot/Programming/C and C++/XWindows/src/MyWindow.cpp:49
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的错误表明
XMapWindow()
的display
或window
参数不正确。您需要在代码中包含更多错误检查,特别是针对
XOpenDisplay
和XCreateWindow
调用的结果。我看到的唯一明显的错误是您将 CWBackPixel 标志传递给 XCreateWindow ,但未初始化属性参数。与普通 C 不同,C++ 在声明结构时不会清除结构的内存内容。
编辑 - 我当然错过了真正令人眼花缭乱的明显错误 - 你无意中在构造函数中重新声明了所有类成员变量。这将是你的范围问题。您需要从构造函数内的所有赋值中删除类型名,例如:
Your error indicates that either the
display
orwindow
parameters toXMapWindow()
were incorrect.You need to include more error checking in your code, in particular for the results of the calls to
XOpenDisplay
andXCreateWindow
.The only obvious error I can see is that you're passing the
CWBackPixel
flag toXCreateWindow
but have left theattributes
parameter uninitialised. Unlike plain C, C++ does not clear the memory contents of structures when they're declared.EDIT - I of course missed the really blindingly obvious error - you've inadvertently redeclared all of your classes member variables within your constructor. That'll be your scope problem. You need to remove the typenames from all of the assignments within the constructor, e.g.: