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.
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?
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".
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).
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 -
Use the constructor explictly.
Don't change the build type (for this reason)
If you want to use implicit construction, comment that macro out.
Last edited by Siddhartha; September 22nd, 2005 at 04:13 AM.
Reason: Spelling...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.