Thread: Problems with wchar_t's

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    4

    Problems with wchar_t's

    Hey,

    I just started programming from home and I'm a little confused. Here's some sample code that I think worked awhile ago:

    Code:
    	
    CString test = "Some Text Here";
    int position = test.FindOneOf("0123456789");
    This gives me a compile error of :

    Code:
    error C2664: 'ATL::CStringT<BaseType,StringTraits>::FindOneOf' : cannot convert parameter 1 from 'const char [11]' to 'const wchar_t *'
    Any ideas why this doesn't work?

    Thanks in advance,
    Eric

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Probably you compiling as UNICODE
    add L before string literals to indicate that this is the UNICODE string
    test.FindOneOf(L"0123456789");
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by vart View Post
    Probably you compiling as UNICODE
    add L before string literals to indicate that this is the UNICODE string
    test.FindOneOf(L"0123456789");
    while on the surface this seems like the solution and probably is, but looking deeper I'd have to question MFC and CString.

    If CString takes an Ansi string during construction and successfully creates an "ANSI" CString, shouldn't it's member functions also be ANSI or know how to deal with it. Even if he is compiling in Unicode, you'd think ANSI CStrings can still be created/used and passed to functions for example that is expecting an ANSI string

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And why do you think that the BaseType is char and not wchar_t? just because CString was succesfully constructed using ANSI String?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Aug 2007
    Posts
    4
    So how would I go about compiling in ANSI instead of UNICODE?

  6. #6
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by vart View Post
    And why do you think that the BaseType is char and not wchar_t? just because CString was succesfully constructed using ANSI String?

    A. Assuming FindOneOf() can take a CString as it's parameter.
    B. Assuming from your statement CString has a conversion constructor that takes a const char*

    FindOneOf( "some const char*") should work.

    In looking at original question, I would guess, this example was contrived for posting the question and not actual code, otherwise I would bet that the first statement would give an error too. Let me go fire up Visual studio and test....

    ***UPDATE**** I am right, the first line gives an error also, i.e. a Unicode CString can't be constructed from a const char*

    To the OP:

    Assuming you are using Visual Studio:

    Menu -> Projects -> Properties -> Configuration Properties -> General Character Set -> Change to "Not Set" ... or use UNICODE Literals as Vart suggested
    Last edited by Darryl; 08-07-2007 at 01:34 PM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    or wrap each literal into TEXT("Hello world!") macro to be able to compile it both ways
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. C Pointers Problems
    By mhelal in forum C Programming
    Replies: 8
    Last Post: 01-10-2007, 06:35 AM
  3. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM