gpt4 book ai didi

c++ - 遇到段错误,在类和变量方面遇到问题

转载 作者:行者123 更新时间:2023-11-28 03:57:49 26 4
gpt4 key购买 nike

好的,所以我仍在学习 C++ 的基本知识,所以如果这是一个简单的错误,我深表歉意。

我有这门课:

class RunFrame : public wxFrame {
public:
RunFrame();
void OnKey(wxKeyEvent& keyEvent);

private:
// Configuration variables.
const wxString *title;
const wxPoint *origin;
const wxSize *size;
const wxColour *background;
const wxColour *foreground;
const wxString *placeholder;

// Control variables.
wxTextCtrl *command;

// Event table.
DECLARE_EVENT_TABLE()
};

...然后在 OnKey 方法中我有这段代码:

void RunFrame::OnKey(wxKeyEvent& keyEvent) {
// Take the key and process it.
if(WXK_RETURN == keyEvent.GetKeyCode()) {
bool empty = command -> IsEmpty();
}

// Propogate the event through.
keyEvent.Skip();
}

...但是当我的程序到达我尝试从命令变量调用 IsEmpty 方法的那一行时,我的程序一直出现段错误。我的问题是,“为什么?”在 RunFrame 类的构造函数中,我似乎可以像在 OnKey 方法中那样调用命令变量的方法……它编译正确,它只是在我尝试执行该行时出现错误.

如有必要,这里是构造函数的代码:

RunFrame::RunFrame() :
wxFrame(NULL, wxID_ANY, wxT("DEFAULT"), wxDefaultPosition, wxDefaultSize, wxBORDER_NONE) {
// Create the styling constants.
title = new wxString(wxT("RUN"));
origin = new wxPoint(0, 0);
size = new wxSize(250, 25);
background = new wxColour(33, 33, 33);
foreground = new wxColour(255, 255, 255);
placeholder = new wxString(wxT("command"));

// Set the styling for the frame.
this -> SetTitle(*title);
this -> SetSize(*size);

// Create the panel and attach the TextControl to it.
wxPanel *panel = new wxPanel(this, wxID_ANY, *origin, *size, wxBORDER_NONE);

// Create the text control and attach it to the panel.
command = new wxTextCtrl(panel, wxID_ANY, *placeholder, *origin, *size);

// Set the styling for the text control.
command -> SetBackgroundColour(*background);
command -> SetForegroundColour(*foreground);

// Connect the key event to the text control.
command -> Connect(wxEVT_CHAR, wxKeyEventHandler(RunFrame::OnKey));

// Set the focus to the command box.
command -> SetFocus();
}

在此先感谢您提供的任何帮助!

问候,天球

最佳答案

您正在捕获 RunFrame 对象以外的对象中的事件。可能它被捕获在 wxFrame 类型的基础对象中。使用运行时命令 wxEvtHandler::Bind<>() 来绑定(bind)事件,而不是事件表,它应该清楚发生了什么。

要验证这是问题所在,请将 RunFrame 对象的地址与 OnKey 方法中的“this”指针进行比较。我猜他们是不同的。

更新:向我们展示您的事件表定义。这就是问题所在。

更新 2:我必须把它留给你。也许这会更清楚:您的事件处理程序被定义为属于 RunFrame。事件表(我认为)将事件绑定(bind)到不同的对象。结果是 RunFrame::OnKey 不是用 RunFrame 对象的 this-pointer 调用的,而是一些其他对象,可能是不同类型的。祝你好运。该走了。

关于c++ - 遇到段错误,在类和变量方面遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2742381/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com