ID_BUTTON 未使用常量表达式初始化
ID_BUTTON was not initialized with constant expression
我想在我的应用程序中使用 "Enter" 和 "Shift" 键。为此,我找到了一些代码 here。
void wxWidgetsDialog::OnKey(wxKeyEvent &event)
{
wxWindow *win = FindFocus();
if(win == NULL){
event.Skip ();
return;
}
if(event.GetKeyCode()==WXK_RETURN){
switch(win->GetId()){
case ID_BUTTON1:
wxMessageBox("Button1");
break;
case ID_BUTTON2:
wxMessageBox("Button2");
break;
case ID_BUTTON3:
wxMessageBox("Button3");
Destroy();
break;
}
event.Skip ();
}
}
当我尝试使用此代码段时,出现错误:
D:\WindowsDgps\WindowsDgpsGUI\PortsDialog.cpp|171|error: the value of 'PortsDialog::ID_BUTTON1' is not usable in a constant expression|
和
D:\WindowsDgps\WindowsDgpsGUI\PortsDialog.cpp|35|note: 'PortsDialog::ID_BUTTON1' was not initialized with a constant expression|
我做错了什么?我必须如何声明 ID 才能使其正常工作?我使用了 wxWidgets 自己生成的标准实现。
const long PortsDialog::ID_STATICTEXT1 = wxNewId();
const long PortsDialog::ID_TEXTCTRL1 = wxNewId();
const long PortsDialog::ID_BUTTON1 = wxNewId();
const long PortsDialog::ID_TEXTCTRL2 = wxNewId();
const long PortsDialog::ID_BUTTON2 = wxNewId();
const long PortsDialog::ID_BUTTON3 = wxNewId();
const long PortsDialog::ID_PANEL1 = wxNewId();
有什么我可以做的,这样我就可以在 StaticText 字段中使用 Enter 键激活按钮吗?
虽然 PortsDialog::ID_BUTTON1
是您无法更改的常量,但它不是 编译时 常量。它的值在编译时不固定,它在 运行 时初始化。
switch
中的 case 必须是编译时常量。
这里不能使用switch
,必须使用if
和else if
链。
我想在我的应用程序中使用 "Enter" 和 "Shift" 键。为此,我找到了一些代码 here。
void wxWidgetsDialog::OnKey(wxKeyEvent &event)
{
wxWindow *win = FindFocus();
if(win == NULL){
event.Skip ();
return;
}
if(event.GetKeyCode()==WXK_RETURN){
switch(win->GetId()){
case ID_BUTTON1:
wxMessageBox("Button1");
break;
case ID_BUTTON2:
wxMessageBox("Button2");
break;
case ID_BUTTON3:
wxMessageBox("Button3");
Destroy();
break;
}
event.Skip ();
}
}
当我尝试使用此代码段时,出现错误:
D:\WindowsDgps\WindowsDgpsGUI\PortsDialog.cpp|171|error: the value of 'PortsDialog::ID_BUTTON1' is not usable in a constant expression|
和
D:\WindowsDgps\WindowsDgpsGUI\PortsDialog.cpp|35|note: 'PortsDialog::ID_BUTTON1' was not initialized with a constant expression|
我做错了什么?我必须如何声明 ID 才能使其正常工作?我使用了 wxWidgets 自己生成的标准实现。
const long PortsDialog::ID_STATICTEXT1 = wxNewId();
const long PortsDialog::ID_TEXTCTRL1 = wxNewId();
const long PortsDialog::ID_BUTTON1 = wxNewId();
const long PortsDialog::ID_TEXTCTRL2 = wxNewId();
const long PortsDialog::ID_BUTTON2 = wxNewId();
const long PortsDialog::ID_BUTTON3 = wxNewId();
const long PortsDialog::ID_PANEL1 = wxNewId();
有什么我可以做的,这样我就可以在 StaticText 字段中使用 Enter 键激活按钮吗?
虽然 PortsDialog::ID_BUTTON1
是您无法更改的常量,但它不是 编译时 常量。它的值在编译时不固定,它在 运行 时初始化。
switch
中的 case 必须是编译时常量。
这里不能使用switch
,必须使用if
和else if
链。