Frage

i am trying to Print some data in wxstring using printf function but its crashing in run time in LINUX but not in windows

here is my code:

wxString str1,str2;
str1 = "Elements";
str2.Printf( _U("%s"),str1);

This is working in windows but not in linux , if i change it below its working in linux also

str2.Printf( _U("%s"),str1.c_str());

why its not taking str1 as argument.

Note:This sentence i am using throughout the workspace is there any common way to do this in linux instead of changing in all places

War es hilfreich?

Lösung

The only "fix" is to upgrade to wxWidgets 3.0 where wxString::Printf() and other similar functions are (pseudo) variadic templates and so do work correctly with objects and not only raw pointers. In wxWidgets 2.8 they are variadic functions and, according to the language rules, can't work with the objects and no, there is no way around this.

Andere Tipps

This help clarify you:

The following code:

wxString str;
str.Printf(wxT("My string is %s"), wxString("whatever"));

does not work. Unfortunately, it may seem to work fine under Windows because of a compiler quirk there but passing a wxString object to a function taking a variable number of arguments such as Printf() is undefined behaviour in C++. Accordingly, it will simply crash under most platforms but may even "work" on some of them. You must use c_str() to make the above code work, i.e. write this instead:

wxString str;
str.Printf(wxT("My string is %s"), wxString("whatever").c_str());

Note that g++ should give you an error when passing an object to a vararg function like this -- another reason to compile your code with g++ even if you normally use another compiler.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top