Deriving from wxTextCtrl Causes Build Errors w/ MSVC

Do you have a question about makefiles, a compiler or IDE you are using and need to know how to set it up for wxWidgets or why it doesn't compile but other IDE's do ? Post your questions here.
Post Reply
mmead
In need of some credit
In need of some credit
Posts: 4
Joined: Tue Jun 08, 2021 7:30 pm

Deriving from wxTextCtrl Causes Build Errors w/ MSVC

Post by mmead »

Hi,

I am running to a strange build error when creating my own class derived from wxTextCtrl when building with MSVC. I am using VS 2019 (latest) and have compiled wxWidgets 3.1.4 from source using CMake.

Code: Select all

$ mkdir build
$ cd build
$ cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=C:/External
$ cmake --build .. --target install
A stripped down version of my code looks like:

Code: Select all

#include <wx/wx.h>
#include <wx/textctrl.h>
#include <wx/valnum.h>
#include <wx/sizer.h>

class MyTextCtrl : public wxTextCtrl {
public:
	explicit MyTextCtrl(wxWindow* parent, int id, int value)
		: wxTextCtrl()
	{
		wxIntegerValidator<int> val(nullptr);
		
		auto str = wxString::Format(wxT("%i"), id);
		this->Create(parent, id, str, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER, val);
	}
	
	virtual ~MyTextCtrl()
	{
	}
	
	int GetInt() const
	{
		long value = 0;
		this->GetValue().ToLong(&value);
		return value;
	}
};

class MyFrame : public wxFrame {
public:
	MyFrame()
		: wxFrame(nullptr, wxID_ANY, wxEmptyString, wxDefaultPosition)
	{
		auto sizer = new wxBoxSizer(wxVERTICAL);
		sizer->Add(new MyTextCtrl(this, wxID_ANY, 50));
	}
	
	virtual ~MyFrame()
	{
	}
};
When compiling, this produces several build errors:

Code: Select all

textentry.h(101, 39): error 2601: syntax identifier 'wxKeyEvent'
textctrl.h(292,18): error C3668: 'wxTextCtrl::MSWProcessSpecialkey': method with override specifier 'override' did not override any base class methods
combobox.h(168,18): error C3668: 'wxComboBox::MSWProcessSpecialKey': method with override specifier 'override' did not override any base class methods
Strangely, this code compiles just fine on Linux. This makes me think the error is with my build configuration (below), but I'm unsure of how to fix it.

Code: Select all

if(MSVC)
	set(wxWidgets_ROOT_DIR "C:/External/wxWidgets-3.1.4")
	find_package(wxWidgets REQUIRED COMPONENTS gl core base)
	include(${wxWidgets_USE_FILE})
	add_compile_definitions(${wxWidgets_DEFINITIONS})
	include_directories(${wxWidgets_INCLUDE_DIRS})
	
	add_executable(MyExe
	WIN32
			main.cpp
	)
	
	target_link_libraries(MyExe
		${wxWidgets_LIBRARIES}
	)
endif()
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Deriving from wxTextCtrl Causes Build Errors w/ MSVC

Post by doublemax »

I can't test with VS2019, but with VS2017 it still compiles.

Can you build wxWidgets and the "minimal" sample with the default configuration, then copy your class to the sample and check if it compiles then?

Just to narrow down if it's the compiler or anything else.
Use the source, Luke!
Post Reply