Microsoft KB Archive/328431

= BUG: You may receive an error message C2664 from the compiler if you call the CStringT constructor and you pass a CFixedStringMgr pointer as an argument in Visual C++ .NET 2003 =

Article ID: 328431

Article Last Modified on 1/6/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition

-



This article was previously published under Q328431





SYMPTOMS
If you call the CStringT constructor and you pass a CFixedStringMgr pointer, you may receive the following C2664 error message from the compiler:

...\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\cstringt.h (875) : error C2664: 'PtrToStringChars' : cannot convert parameter 1 from 'ATL:: CFixedStringMgr *' to 'const System::String __gc *' Cannot convert an unmanaged type to a managed type MySample.cpp(7) : see reference to function template instantiation 'ATL: :CStringT::CStringT(SystemString *) ' being compiled with

[

BaseType=char,

StringTraits=ATL::StrTraitATL,

SystemString=ATL::CFixedStringMgr ]

MySample.cpp(5) : while compiling class-template member function 'A::A(void) throw'

with

[ StringType=ATL::CAtlString ]

MySample.cpp(15) : see reference to class template instantiation 'A' being compiled

with

[ StringType=ATL::CAtlString ]



CAUSE
You receive this error message because the compiler matches the wrong constructor. The compiler should match the IATLStringMgr* constructor, however, the System::String* constructor is invoked instead.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



WORKAROUND
When you call the constructor, cast the CFixedStringMgr pointer to the IAtlStringMgr pointer. The code to call the constructor is as follows: A throw : CFixedStringMgr( &m_data, 1024, StrTraits::GetDefaultManager ), StringType( static_cast( this ) ) Note You may receive two LNK4089 linker warnings. These linker warnings are expected, and you can safely ignore them. Use one of the following two methods to avoid the LNK4089 warning:
 * Use the linker switch /OPT:NOREF. This turns off the linker optimizations. The result is a larger image.
 * Use the linker switch /IGNORE:4089. This disables warning LNK4089



Steps to reproduce the behavior
 Click Start, point to Programs, point to Visual Studio .NET 2003, point to Visual Studio .NET Tools, and then click Visual Studio .NET 2003 Command Prompt.  Paste the following program code in Notepad or in another text editor:
 * 1) include <atlstr.h>

template <class StringType> class A : private ATL::CFixedStringMgr, public StringType { public: A throw : CFixedStringMgr( &m_data, 1024, StrTraits::GetDefaultManager ), StringType( static_cast<CFixedStringMgr* >( this ) ) {   }    CStringData m_data; typename StringType::XCHAR m_achData[1024]; };

int main(void) {   A<CAtlString> s;    return 0; } </li> Save the file as MySample.cpp .</li>  To compile the code, run following command from the Visual Studio .NET 2003 command prompt: cl /clr MySample.cpp </li></ol>

You may receive the compilation error that is described in the &quot;Symptoms&quot; section.

Keywords: kbbug kbcompiler KB328431

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.