Why do I have to add casts in the code generated by wxrc?

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
bertolino
Experienced Solver
Experienced Solver
Posts: 77
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Why do I have to add casts in the code generated by wxrc?

Post by bertolino »

Hello all,
I've been using wxrc to turn my resources into source files for many years. I call it like this:

Code: Select all

./wxrc application_resources.xml -v -e -c -o application_resources.cpp
As you know, wxrc creates a class for each resource like in the example below (application_resources.h):

Code: Select all

class bitmapLogo : public wxBitmap {
protected:

private:
 void InitWidgetsFromXRC(wxWindow *parent){
  wxXmlResource::Get()->LoadObject(this,parent,wxT("bitmapLogo"), wxT("wxBitmap"));
 }
public:
bitmapLogo(wxWindow *parent=NULL){
  InitWidgetsFromXRC((wxWindow *)parent);
 }
};
Everything is fine but the thing is that if I want this code to compile, I have to manually cast the calls

Code: Select all

Get()->LoadObject(this, ... 
into

Code: Select all

Get()->LoadObject((wxObject*)this, ...
It's not a big deal but after many years I'm fed up with this extra step and I guess there is a trick to avoid this manual cast. Did I miss something?
Many thanks for your help.

Pascal
User avatar
doublemax
Moderator
Moderator
Posts: 19115
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Why do I have to add casts in the code generated by wxrc?

Post by doublemax »

What compiler are you using and what's the exact error message you're getting?

As wxBitmap derives from wxObject and there are no other matching overloads, i don't see why the cast should be necessary. And if i try to compile the code in VS2013, it works fine without the cast.
Use the source, Luke!
bertolino
Experienced Solver
Experienced Solver
Posts: 77
Joined: Wed Aug 14, 2013 8:07 am
Location: France
Contact:

Re: Why do I have to add casts in the code generated by wxrc?

Post by bertolino »

Thanks doublemax for your answer.

I use Visual studio 2019 Pro (and I also had the problem with Visual Studio 2015) and its Microsoft compiler.
Actually, your answer pushed me to really dig into the error message which is:

Code: Select all

1>application_resources.h(1080,1): error C2664: 'bool wxXmlResource::LoadObject(wxObject *,wxWindow *,const wxString &,const wxString &)': cannot convert argument 1 from 'IDR_CITE *' to 'wxObject *'
1>application_resources.h(1080,36): message : Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\wxWidgets\include\wx\xrc\xmlres.h(208,10): message : see declaration of 'wxXmlResource::LoadObject'
This resource refers to an HTML file:

Code: Select all

<object class="wxString" name="IDR_CITE">cite.html</object>
And is loaded as:

Code: Select all

wxString buffer = wxLoadUserResource(wxT("IDR_CITE"), wxT("TEXT"));
I also found out that this resource was used inside a dead code (shame on me)!
To conclude, no more problems with wxrc. I removed the bad part of code and all the other resources compile correctly. Sorry for the inconvenience and again thank you for your availability.
Post Reply