CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Using CString in a console-application

    Hello!

    I used the wizard to create a Win32-Console-Application, but with MFC-support to use classes like CString.

    Now I cannot compile lines like:
    CString s = "Hello";
    (error C2440: 'initializing' : cannot convert from 'const char [6]' to 'ATL::CStringT<BaseType,StringTraits>')

    But I can compile lines like:
    CString s("Hello");

    I have some code I want to re-use which has function-deklarations like:
    void MyFund(CString sParam = "default")
    which don't work anymore as well, but which work fine of course in a MFC/dialog-based-application for example.

    Any idea what I can do?

  2. #2
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Using CString in a console-application

    Quote Originally Posted by martho
    I used the wizard to create a Win32-Console-Application, but with MFC-support to use classes like CString.

    Now I cannot compile lines like:
    CString s = "Hello";
    (error C2440: 'initializing' : cannot convert from 'const char [6]' to 'ATL::CStringT<BaseType,StringTraits>')

    Any idea what I can do?
    I don't understand what you have done - for me, the code you posted works fine in a Win32 console app with MFC support (according to the error message, I assume you are using VS 2003). What exactly were the steps you used to create that project?

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Using CString in a console-application

    Try the attached project. It works perfectly for me (VC++ 6.0).
    Attached Files Attached Files
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815

    Re: Using CString in a console-application

    Quote Originally Posted by cilu
    Try the attached project. It works perfectly for me (VC++ 6.0).
    Note the error message he got: "cannot convert from 'const char [6]' to 'ATL::CStringT<BaseType,StringTraits>'" This looks like VC++ 7.x.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Using CString in a console-application

    Yes, CStringT... I missed that. VC++7.x indeed.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: Using CString in a console-application

    Thanks for your replys.

    Sorry, yes it's VC++ 7.x. I tried it as well in VC++ 8 Beta 2 with the same result.

    I fixed it with using "Use Multi-Byte character set" instead of "Use Unicode charatcer set" in the project-properties, but I got no explanaition for it. The wizard chose the "unicode-set".

  7. #7
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Using CString in a console-application

    Quote Originally Posted by martho
    CString s = "Hello";
    (error C2440: 'initializing' : cannot convert from 'const char [6]' to 'ATL::CStringT<BaseType,StringTraits>')

    But I can compile lines like:
    CString s("Hello");
    This behaviour is correct, and by design for UNICODE builds.

    The reason is that you are using the CStringT constructor implicitly for converting from normal character strings to a (CString wrapped) Unicode string.

    To use implicit conversions, you need to comment the macro definition -
    Code:
    // #define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS  // Comment this out to support implicit conversions
    inside your StdAfx.h

    When you do this, you will see that both lines compile.

    Else, only the latter will.

    However, note that the best way to construct objects is explicitly, and not implicitly (hence, the latter is disabled by default).

    To understand this topic some more, look up -


  8. #8
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Using CString in a console-application

    Quote Originally Posted by martho
    I fixed it with using "Use Multi-Byte character set" instead of "Use Unicode charatcer set" in the project-properties, but I got no explanaition for it.
    The reason why this fixed your problem is that with MBCS (correctly said: without UNICODE) there is nothing implicit about the lines-
    Code:
    CString s = "Test":
    or
    Code:
    CString s ("Test");
    In both cases, you are using CString to wrap a normal character string given a normal character string input.

    Unlike in the Unicode build where you were using CString implicitly to wrap a Unicode string given a normal character string input.

    To simulate the error behaviour for MBCS build, do the converse -
    Code:
    // Will not compile for MBCS
     CString s_unicode = L"Test";
     
     // This will compile
     CString s_unicode_2 (L"Test");
    As you can see, the macro _ATL_CSTRING_EXPLICIT_CONSTRUCTORS intends to keep the user from performing unintentional conversions.

    Solution to the problem -

    1. Use the constructor explictly.
    2. Don't change the build type (for this reason)
    3. If you want to use implicit construction, comment that macro out.
    Last edited by Siddhartha; September 22nd, 2005 at 04:13 AM. Reason: Spelling...

  9. #9
    Join Date
    Aug 1999
    Location
    Germany
    Posts
    2,338

    Re: Using CString in a console-application

    Thanks a lot for the explanation!

  10. #10
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: Using CString in a console-application

    You are welcome...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured