none
Error when try to add string to MFC ListBox RRS feed

  • Question

  • Hi all,
    My code:

    v_listbox.AddString("Try to add string to MFC listbox");


    when I compiled above code I got this error message:
    "error C2664: 'CListBox::AddString' : cannot convert parameter 1 from 'const char [33]' to 'LPCTSTR'. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast."

    After I changed above code to:

    v_listbox.AddString((LPCTSTR)"Try to add string to MFC listbox");

    Program compiled successfull, but it print out some random chinese characters.

    Can anyone help me with this problem?  (what compile parameter need to add to remove this error message?)

    Thanks
    Johny
    Wednesday, March 15, 2006 6:44 PM

Answers

  • You have a Unicode build. Use the _T macro for string literals :

    v_listbox.AddString(_T("Try to add string to MFC listbox"));

    See http://msdn2.microsoft.com/en-us/library/c426s321.aspx for more info.

     JohnyTho wrote:
    Hi all,
    My code:

    v_listbox.AddString("Try to add string to MFC listbox");


    when I compiled above code I got this error message:
    "error C2664: 'CListBox::AddString' : cannot convert parameter 1 from 'const char [33]' to 'LPCTSTR'. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast."

    After I changed above code to:

    v_listbox.AddString((LPCTSTR)"Try to add string to MFC listbox");

    Program compiled successfull, but it print out some random chinese characters.

    Can anyone help me with this problem?  (what compile parameter need to add to remove this error message?)

    Thanks
    Johny

    Wednesday, March 15, 2006 7:16 PM
    Moderator

All replies

  • You have a Unicode build. Use the _T macro for string literals :

    v_listbox.AddString(_T("Try to add string to MFC listbox"));

    See http://msdn2.microsoft.com/en-us/library/c426s321.aspx for more info.

     JohnyTho wrote:
    Hi all,
    My code:

    v_listbox.AddString("Try to add string to MFC listbox");


    when I compiled above code I got this error message:
    "error C2664: 'CListBox::AddString' : cannot convert parameter 1 from 'const char [33]' to 'LPCTSTR'. Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast."

    After I changed above code to:

    v_listbox.AddString((LPCTSTR)"Try to add string to MFC listbox");

    Program compiled successfull, but it print out some random chinese characters.

    Can anyone help me with this problem?  (what compile parameter need to add to remove this error message?)

    Thanks
    Johny

    Wednesday, March 15, 2006 7:16 PM
    Moderator
  • Nishant Sivakumar, thanks for your help. Problem solved after I followed your suggestion .
    Thursday, March 16, 2006 2:56 AM
  • I have tried use .AddString(_T("abc")) but the systm still give me some funny Chinese like characters. and the listbox display nothing. I am quite new to MFC. Thanks in advance for anyone who could help!
    Tuesday, September 4, 2007 8:54 AM