[wxString] problem when initializing Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

[wxString] problem when initializing

Post by KaReL » Wed Dec 06, 2006 7:57 am

Hi,


I have a problem initializing a wxString...
wxUSE_UNICODE=0
wxUSE_WCHAR_T=1

wxString str( "Hello" );
==> cannot convert parameter 1 from 'const char [6]' to 'int'
wxString str = "Hello";
==> cannot convert from 'const char [6]' to 'wxString'
std::string s("Hello");
wxString str = s.c_str();
==> cannot convert from 'const char *' to 'wxString'

But when I use:
wxString str = wxT("Hello");
==> Everything works fine...


When I look at the preprocessed file (.i), I see that wxChar got typedef'ed to char, I see wxString has a constructor const wxChar * psz... So I am really puzzled here why this is happening!


Hope that someone can help me :(


Greetz,
Steven
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

frank_frl
Earned some good credits
Earned some good credits
Posts: 139
Joined: Sat Feb 18, 2006 1:41 pm
Location: Germany

Post by frank_frl » Wed Dec 06, 2006 8:18 am

Hi KaReL,

when you define wxUSE_WCHAR_T = 1 a character must be 2 bytes size. The macro wxT() is doing the conversation, that's why that works.

Regards,

Frank

sitsuj
Knows some wx things
Knows some wx things
Posts: 35
Joined: Sun Nov 26, 2006 9:02 am
Location: Lodz, Poland

Post by sitsuj » Wed Dec 06, 2006 8:41 am

Maybe you should try simple type casting? I had a similar problem and now it works fine.

Code: Select all

wxString login_value = (wxString)"your login";
Hope this will work :) Regards -

sitsuj

S.Volkenandt
Knows some wx things
Knows some wx things
Posts: 26
Joined: Tue Nov 14, 2006 1:51 pm
Location: Duesseldorf, Germany

Post by S.Volkenandt » Wed Dec 06, 2006 8:54 am

This will again only work if the character type is only one byte wide. In general, you're safest when you mark all literal strings with wxT(), since this works fine in both Unicode and ANSI modes.

More precisely:
A narrow character string in C++ is represented by the text within quotation marks. A wide character string is represented likely, but with an L prefixing the first quotation mark. The wxT macro makes sure that there is an L in Unicode mode, and that there isn't in ANSI mode.

Code: Select all

char const* ansi = "Hello, World"; // ANSI
wchar_t const* unicode = L"Hello, World"; // Unicode
wxChar const* whatever = wxT("Hello, World"); // Expands to what's appropriate

KaReL
Experienced Solver
Experienced Solver
Posts: 78
Joined: Mon Aug 30, 2004 8:52 am
Contact:

Post by KaReL » Wed Dec 06, 2006 10:26 am

Ok,


I don't know what's wrong. Let's just blame Microsoft. I did a complete rebuild of the project, and now it seems that wxChar is being converted to wchar_t in the preprocessed file...

So I found the solution now (provide a wxConv).


Greetz & thx!
wxWidgets: SVN/trunk
OS: WinXP/2 + Ubuntu + Mac 10.4.11
Compiler: VS2005 + GCC 4.2 + GCC 4.0.1
-----
home: http://www.salvania.be

Post Reply