【发布时间】:2021-11-12 10:56:25
【问题描述】:
我是 C++ 和 wxWidgets 的新手,并试图遵循一个基本示例,除了由 标记的 OnButtonClicked 之外,该示例似乎可以运行
在构建阶段变量未定义(如果我注释掉这两行代码将运行)
变量m_list1和m_txt1的定义有问题吗?
//===================cMain.h ===========
#pragma once //see 22:450
#include "wx/wx.h"
class cMain : public wxFrame
{
public:
cMain();
~cMain();
public:
wxButton *m_btn1 = nullptr; //25:48
wxTextCtrl *m_txt1 = nullptr;
wxListBox *m_list1 = nullptr;
void OnButtonClicked(wxCommandEvent &evt); //27:30
wxDECLARE_EVENT_TABLE(); // 27:34
};
//===================cMain.cpp ===========
#include "cMain.h"
wxBEGIN_EVENT_TABLE(cMain,wxFrame) // 28:00
//EVT_BUTTON(10001,OnButtonClicked) //28:33
wxEND_EVENT_TABLE()
cMain::cMain() : wxFrame(nullptr, wxID_ANY, "Peter's First WxWidget Code ",wxPoint(30,30),wxSize(800,600))
{
m_btn1 = new wxButton(this, 10001, "Click Me", wxPoint(10, 10), wxSize(150, 50)); //28:36
m_txt1=new wxTextCtrl(this, wxID_ANY, "", wxPoint(10, 70), wxSize(300, 30));
m_list1=new wxListBox(this, wxID_ANY, wxPoint(10, 110), wxSize(300,300));
}
cMain::~cMain()
{
}
void OnButtonClicked(wxCommandEvent &evt)
{
//m_list1->AppendString(m_txt1->GetValue()); //<<<<<<<<<<<<<<<
m_list1->AppendString("ONE"); //<<<<<<<<<<<<<<<
evt.Skip(); //29.24
}
//===================cApp.h ===========
#pragma once //24:09
#include "wx/wx.h"
#include "cMain.h"
class cApp : public wxApp
{
public:
cApp();
~cApp();
private:
cMain* m_frame1 = nullptr;
public:
virtual bool OnInit();
};
//===================cApp.cpp ===========
#include "cApp.h" // 24:19
wxIMPLEMENT_APP(cApp);
cApp::cApp()
{
}
cApp::~cApp()
{
}
bool cApp::OnInit()
{
m_frame1 = new cMain();
m_frame1->Show();
return true;
}
任何帮助表示赞赏
【问题讨论】:
-
请复制粘贴错误信息编译器/链接器报告!请注意,标题中的这个
<<<<<只是一个噪音,所以请考虑更好的标题。 -
我的 C++真的生锈了,但看起来
OnButtonClicked不是该类的成员,因此它无法独立访问该类 public特性。您需要一个实例(或通过将cMain::放在名称前面来使OnButtonClicked成为该类的成员)。 -
错别字,应该是:
void cMain::OnButtonClicked(wxCommandEvent &evt) -
在类定义之外,成员函数名需要以类名和
::(作用域)运算符作为前缀。例如,在类定义之外定义void OnButtonClicked(wxCommandEvent &evt)时,需要为void cMain::OnButtonClicked(wxCommandEvent &evt)。 [并且,不要猜测,而是尝试阅读会告诉您这些事情的基本文本]。 -
@PeterWilliams,最重要的是其他人所说的 -
EVT_BUTTON(10001,OnButtonClicked)真的应该是EVT_BUTTON(10001,cMain::OnButtonClicked)。
标签: c++ visual-studio wxwidgets