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 (328431)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2003)

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<BaseType,StringTraits>::CStringT<ATL::CFixedStringMgr>(SystemString *) ' being compiled with
[
BaseType=char,
StringTraits=ATL::StrTraitATL<char,ATL::ChTraitsCRT<char>>,
SystemString=ATL::CFixedStringMgr ]
MySample.cpp(5) : while compiling class-template member function 'A<Stri ngType>::A(void) throw()'
with
[ StringType=ATL::CAtlString ]
MySample.cpp(15) : see reference to class template instantiation 'A<Stri ngType>' 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 "Applies to" 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<IAtlStringMgr*  >( 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

MORE INFORMATION

Steps to reproduce the behavior

  1. 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.
  2. Paste the following program code in Notepad or in another text editor:
    #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;
    }
    
  3. Save the file as MySample.cpp.
  4. To compile the code, run following command from the Visual Studio .NET 2003 command prompt:
    cl /clr MySample.cpp
You may receive the compilation error that is described in the "Symptoms" section.

Modification Type:MinorLast Reviewed:1/6/2006
Keywords:kbCompiler kbbug KB328431 kbAudDeveloper