我试图将我的应用程序中某一页面的所有 GUI 封装在一个函数中,以便我的 mainFrame 函数更加干净,这样我就可以通过仅调用一个函数来创建整个 Gui,但是我收到了 C2064 错误当我尝试使 mainFrame 作为按钮的事件处理程序时,有人可以帮忙吗?下面我提供了我的脚本:
主机Cpp文件:
#include "MainFrame.h"
#include <wx/wx.h>
#include <wx/scrolwin.h>
#include <fstream>
void CollectCustomerInfoGUI(wxPanel* BG) {
MainFrame* mainFrame;
wxTextCtrl* CustomerName = new wxTextCtrl(BG, wxID_ANY, "Enter Name", wxPoint(380, 150), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
wxFont InfoFont = CustomerName->GetFont();
InfoFont.SetPointSize(17);
CustomerName->SetFont(InfoFont);
CustomerName->SetBackgroundColour("#04BF68");
CustomerName->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerGender = new wxTextCtrl(BG, wxID_ANY, "Male / Female", wxPoint(380, 220), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerGender->SetFont(InfoFont);
CustomerGender->SetBackgroundColour("#04BF68");
CustomerGender->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerAddress = new wxTextCtrl(BG, wxID_ANY, "Enter Adress", wxPoint(380, 290), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerAddress->SetFont(InfoFont);
CustomerAddress->SetBackgroundColour("#04BF68");
CustomerAddress->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerAge = new wxTextCtrl(BG, wxID_ANY, "Enter Age", wxPoint(380, 360), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerAge->SetFont(InfoFont);
CustomerAge->SetBackgroundColour("#04BF68");
CustomerAge->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerMobileNum = new wxTextCtrl(BG, wxID_ANY, "Enter Mobile Number", wxPoint(380, 430), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerMobileNum->SetFont(InfoFont);
CustomerMobileNum->SetBackgroundColour("#04BF68");
CustomerMobileNum->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerCreditCardNumber = new wxTextCtrl(BG, wxID_ANY, "Enter Credit Card Number", wxPoint(380, 500), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerCreditCardNumber->SetFont(InfoFont);
CustomerCreditCardNumber->SetBackgroundColour("#04BF68");
CustomerCreditCardNumber->SetForegroundColour("#F0F1F2");
wxTextCtrl* CustomerID = new wxTextCtrl(BG, wxID_ANY, "EnterID", wxPoint(380, 570), wxSize(700, 35), wxTE_CENTER | wxNO_BORDER | wxTE_NO_VSCROLL);
CustomerID->SetFont(InfoFont);
CustomerID->SetBackgroundColour("#04BF68");
CustomerID->SetForegroundColour("#F0F1F2");
wxButton* CustomerInfoButton = new wxButton(BG, wxID_ANY, "Submit Data", wxPoint(580, 645), wxSize(300, 60), wxNO_BORDER);
CustomerInfoButton->SetFont(InfoFont);
CustomerInfoButton->SetBackgroundColour("#01261C");
CustomerInfoButton->SetForegroundColour("#04BF68");
CustomerInfoButton->SetFocus();
CustomerName->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerGender->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerAddress->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerAge->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerMobileNum->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerCreditCardNumber->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
CustomerID->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
/* with my trial and error I have found the root of cause to be this line*/ CustomerInfoButton->Bind(wxEVT_BUTTON, std::bind(&MainFrame::CustomerInfoBTN, mainFrame, std::placeholders::_1, CustomerName, CustomerGender, CustomerAddress, CustomerAge, CustomerMobileNum, CustomerCreditCardNumber, CustomerID));
}
MainFrame::MainFrame(const wxString& title) : wxFrame(nullptr, wxID_ANY, title) {
wxPanel* BG = new wxPanel(this, wxID_ANY, wxDefaultPosition, wxDefaultSize);
BG->SetBackgroundColour("#038C73");
wxStaticText* AgencyName = new wxStaticText(BG, wxID_ANY, ">>>> Muhammad's LLC <<<<", wxPoint(175, 25), wxSize(1100, 90), wxALIGN_CENTER_HORIZONTAL);
AgencyName->SetForegroundColour("#FDFFFF");
wxFont font = AgencyName->GetFont();
font.SetPointSize(35);
AgencyName->SetFont(font);
BG->Bind(wxEVT_PAINT, &MainFrame::OnPaint, this);
CollectCustomerInfoGUI(BG);
}
void MainFrame::OnPaint(wxPaintEvent& event) {
wxPanel* BG = dynamic_cast<wxPanel*>(event.GetEventObject());
if (!BG)
return;
wxPaintDC dc(BG); // Use a wxPaintDC to draw on the panel
// Set the drawing color
dc.SetPen(wxPen(wxColour("#038C73"), 2)); // Blue color with 2-pixel width
dc.SetBrush(wxBrush(wxColour("#025949"), wxBRUSHSTYLE_SOLID));
// Draw a rectangle at position (10, 10) with width 100 and height 50
dc.DrawRoundedRectangle(15, 125, 1420, 610, 10);
}
void MainFrame::StringEmptier(wxFocusEvent& event)
{
// Clear the text when the wxTextCtrl receives focus
wxTextCtrl* textCtrl = dynamic_cast<wxTextCtrl*>(event.GetEventObject());
if (textCtrl) {
textCtrl->SetValue("");
}
event.Skip();
}
void MainFrame::CustomerInfoBTN(wxCommandEvent& event, wxTextCtrl* CustmeName, wxTextCtrl* CustmeGender, wxTextCtrl* CustmeAddress, wxTextCtrl* CustmeAge, wxTextCtrl* CustmeMobileNum, wxTextCtrl* CustmeCreditNum, wxTextCtrl* CustmeIdD) {
wxTextCtrl* CstmerName = CustmeName;
wxTextCtrl* CstmerGender = CustmeGender;
wxTextCtrl* CstmerAddress = CustmeAddress;
wxTextCtrl* CstmerAge = CustmeAge;
wxTextCtrl* CstmerMobileNum = CustmeMobileNum;
wxTextCtrl* CstmerCreditNum = CustmeCreditNum;
wxTextCtrl* CstmerIDDD = CustmeIdD;
Customers CsmClass;
CstmerName->Show(false);
CstmerGender->Show(false);
CstmerAddress->Show(false);
CstmerAge->Show(false);
CstmerMobileNum->Show(false);
CstmerCreditNum->Show(false);
CstmerIDDD->Show(false);
std::ofstream out("old-customers.txt", std::ios::app); {
CsmClass.Name = CstmerName->GetValue().ToStdString();
CsmClass.Gender = CstmerGender->GetValue().ToStdString();
CsmClass.Address = CstmerAddress->GetValue().ToStdString();
CsmClass.Age = std::stoi(CstmerAge->GetValue().ToStdString());
CsmClass.MobileNum = std::stoi(CstmerMobileNum->GetValue().ToStdString());
CsmClass.CreditCardNum = std::stoi(CstmerCreditNum->GetValue().ToStdString());
CsmClass.CustomerID = std::stoi(CstmerIDDD->GetValue().ToStdString());
}
out << "\nName: " << CsmClass.Name << "\nGender: " << CsmClass.Gender << "\nAddress: " << CsmClass.Address << "\nAge: " << CsmClass.Age
<< "\nMobile Number: " << CsmClass.MobileNum << "\nCredit Card Number: " << CsmClass.CreditCardNum << "\nCustomer ID: " << CsmClass.CustomerID;
out.close();
wxMessageBox("Customer info has been succesfully stored!" ,"INFO", wxOK | wxICON_INFORMATION, this);
}
还有我的 MainFrame.h 文件:
#pragma once
#include <WX/WX.h>
#include <wx/scrolwin.h>
#include <iostream>
#include <iomanip>
#include <Windows.h>
class MainFrame : public wxFrame
{
public:
MainFrame(const wxString& title);
void StringEmptier(wxFocusEvent& event);
void CustomerInfoBTN(wxCommandEvent& event, wxTextCtrl* CustmeName, wxTextCtrl* CustmeGender, wxTextCtrl* CustmeAddress, wxTextCtrl* CustmeAge, wxTextCtrl* CustmeMobileNum, wxTextCtrl* CustmeCreditNum, wxTextCtrl* CustmeIdD);
private:
void OnPaint(wxPaintEvent& event);
};
class Customers {
public:
std::string Name{};
std::string Gender{};
std::string Address{};
int Age{};
int MobileNum{};
int CreditCardNum{};
int CustomerID{};
};
class Cabs {
};
class Booking {
};
class Charges {
};
来自 Visual Studio 输出终端的确切错误:
Build started at 7:14 PM...
1>------ Build started: Project: TravelManager, Configuration: Debug x64 ------
1>MainFrame.cpp
1>C:\Users\Muhammad Hadi\Downloads\wxWidgets-3.2.4\include\wx\event.h(547,9): error C2064: term does not evaluate to a function taking 1 arguments
1>(compiling source file 'MainFrame.cpp')
1>C:\Users\Muhammad Hadi\Downloads\wxWidgets-3.2.4\include\wx\event.h(547,9):
1>the template instantiation context (the oldest one first) is
1> D:\C++\TravelManager\TravelManager\MainFrame.cpp(44,17):
1> see reference to function template instantiation 'void wxEvtHandler::Bind<wxEventTypeTag<wxFocusEvent>,void(__cdecl MainFrame::* )(wxFocusEvent &)>(const EventTag &,const Functor &,int,int,wxObject *)' being compiled
1> with
1> [
1> EventTag=wxEventTypeTag<wxFocusEvent>,
1> Functor=void (__cdecl MainFrame::* )(wxFocusEvent &)
1> ]
1> C:\Users\Muhammad Hadi\Downloads\wxWidgets-3.2.4\include\wx\event.h(3888,36):
1> see reference to class template instantiation 'wxEventFunctorFunctor<EventTag,Functor>' being compiled
1> with
1> [
1> EventTag=wxEventTypeTag<wxFocusEvent>,
1> Functor=void (__cdecl MainFrame::* )(wxFocusEvent &)
1> ]
1> C:\Users\Muhammad Hadi\Downloads\wxWidgets-3.2.4\include\wx\event.h(538,18):
1> while compiling class template member function 'void wxEventFunctorFunctor<EventTag,Functor>::operator ()(wxEvtHandler *,wxEvent &)'
1> with
1> [
1> EventTag=wxEventTypeTag<wxFocusEvent>,
1> Functor=void (__cdecl MainFrame::* )(wxFocusEvent &)
1> ]
1>Done building project "TravelManager.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
========== Build completed at 7:14 PM and took 03.092 seconds ==========
我尝试通过在调用函数时请求大型机的引用来设置 MainFrame 事件处理程序,但它只是导致弹出 C2064 错误
当您将指针传递给成员函数时,例如
StringEmptier
,您还需要传递对象来调用它,例如在这种情况下是this
(但不一定总是this
,它也可能是其他对象)。所以你所有的电话都喜欢
CustomerName->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier);
实际上应该是
CustomerName->Bind(wxEVT_SET_FOCUS, &MainFrame::StringEmptier, this);
相反。
顺便说一句,当错误消息中直接有行号时,使用“反复试验”来查找有错误的行是很奇怪的。