Page 1 of 1

[wxString] problem when initializing

Posted: Wed Dec 06, 2006 7:57 am
by KaReL
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

Posted: Wed Dec 06, 2006 8:18 am
by frank_frl
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

Posted: Wed Dec 06, 2006 8:41 am
by sitsuj
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

Posted: Wed Dec 06, 2006 8:54 am
by S.Volkenandt
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

Posted: Wed Dec 06, 2006 10:26 am
by KaReL
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!