Like Tree1Likes
  • 1 Post By kmdv

variable name prepended with L by compiler???

This is a discussion on variable name prepended with L by compiler??? within the C++ Programming forums, part of the General Programming Boards category; I'm checking out wxwidgets (I'm not sure if my problem has to do with it or not). Code: Window::Window (string ...

  1. #1
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    variable name prepended with L by compiler???

    I'm checking out wxwidgets (I'm not sure if my problem has to do with it or not).

    Code:
    Window::Window (string &username) : wxFrame (NULL, mnID_MAIN, wxT("Mnote2"))
    {
    	wxBoxSizer *szr = new wxBoxSizer(wxVERTICAL);
    	SetSizer(szr);
    
    cerr << username << endl;
    
    	this->status = new wxStatusBar(this, -1);
    	szr->Add(this->status);
    
    	wxString remoteuser = wxT(username.c_str());
    	wxStaticText *user = new wxStaticText(this, -1, remoteuser);
    	szr->Add(user);
    
    }
    The line in red causes this from g++:
    mainwindow.cpp:15:1: error: ‘Lusername’ was not declared in this scope

    What is that? There is no "Lusername" in the file at all. If I comment that line out, compiles fine and the cerr prints out username correctly. If I do this:

    Code:
    	string test("hello");
    	wxString remoteuser = wxT(test.c_str());
    I now get 'Ltest was not declared. WTF??
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  2. #2
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Can you find the definition of wxT? Might be a macro.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by threahdead View Post
    Can you find the definition of wxT? Might be a macro.
    Yeah, and:

    Code:
    /* define wxT() and related macros */
    /* ---------------------------------------------------------------------------- */
    
    #if wxUSE_UNICODE
        /* use wxCONCAT_HELPER so that x could be expanded if it's a macro */
        #define wxT(x) wxCONCAT_HELPER(L, x)
    #else /* !Unicode */
        #define wxT(x) x
    #endif /* Unicode/!Unicode */
    
    /*
    Oh well. The wxWidgets docs and wiki actually do not say anything about how to convert from std::string, which is odd considering it is a C++ library.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    Hm, i dont know the wxWidgets library. But it seems this does only work for char arrays.
    You maybe could just do the assignment without the wxT() call. As you can see it only prepends a 'L' if using unicode and returns the argument itself when not using unicode.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300

    Angry

    Quote Originally Posted by threahdead View Post
    Hm, i dont know the wxWidgets library. But it seems this does only work for char arrays.
    You maybe could just do the assignment without the wxT() call.
    No, that causes what the wiki explains is a unicode error. But there is a non-macro, wxString::FromAscii(), that works. Phew. The wx docs are mostly good but it is very silly that they do not just explain this at the top of the page for "wxString" instead of letting you shoot your foot and go running all over the web for half an hour. Grrrrr. Grrrr.
    Last edited by MK27; 04-27-2011 at 07:38 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Everything is clearly explained in the documentary.

    String functions

    It is just a neat macro, which is to be used with string CONSTANTS. There exists an implicit conversion from std::string to wxString, but the problem is that you mix unicode and ansi. Note that you use unicode build for wxWidgets (because wxT expands to L) and std::string (ansi).

    Of course, you can mix ansi and unicode, but you need to use a different constructor for wxString, and use std::string::c_str() explicitly:

    wxString (the last one).

    EDIT:

    Yes, or the mentioned wxString::FromAscii()
    Last edited by kmdv; 04-27-2011 at 07:48 AM.
    stahta01 likes this.
    I never put signature, but I decided to make an exception.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    ‘Lusername’ was not declared in this scope
    It's pronounced "Losername"

    I haven't done much C++ lately, but if I remember correctly, putting L infront of a string makes it unicode. So you would do something like this:
    Code:
    std::wstring unicode = L"This is unicode";
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,265
    Quote Originally Posted by cpjust View Post
    Code:
    ‘Lusername’ was not declared in this scope
    I haven't done much C++ lately, but if I remember correctly, putting L infront of a string makes it unicode.
    Not quite. The L prefix makes the string literal wide, which means it is represented as an array of type wchar_t (which is an implementation defined wide character type). Unicode is a standardised character encoding. Unicode characters can be stored in a wchar_t, but a wchar_t is not necessarily unicode.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 04-16-2011, 12:28 PM
  2. Compiler treating a function as a variable
    By Virmitio in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2008, 02:25 PM
  3. Replies: 2
    Last Post: 02-04-2008, 01:34 AM
  4. Replies: 4
    Last Post: 10-08-2007, 11:47 AM
  5. variable sized arrays: compiler specific?
    By dirkduck in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2002, 10:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21