FIX: Conversion to System:Object May Result in C2594 Ambiguity Error Message (324042)



The information in this article applies to:

  • Microsoft Visual C++ .NET (2002)

This article was previously published under Q324042

SYMPTOMS

When you try to convert a pointer to a managed object to System::Object *, you may receive the following error message from the compiler:
error C2594: 'initializing' : ambiguous conversions from 'namespace::Derived __gc *' to 'System::Object __gc *'

CAUSE

This behavior occurs when the following two conditions are true:
  • The managed pointer is of a class that derives from a class named Object.

    -and-
  • Precompiled header options are turned on.
In this situation, the compiler cannot distinguish between the System::Object and the base Object class, so the compiler incorrectly issues the C2594 error.

RESOLUTION

To work around the problem, rename the base class to something other than Object.

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article. This bug was corrected in Microsoft Visual C++ .NET (2003).

MORE INFORMATION

The following code demonstrates the problem:
//stdafx.h
//include file for standard system include files


//stdafx.cpp
//Compiler option required: /Yc "Stdafx.h" /clr
#include "stdafx.h"

//test.cpp
//Compiler option required: /Yu "stdafx.h" /clr
#include "stdafx.h"
#using <mscorlib.dll>

using namespace System;

namespace NS
{
	public __gc class Object
	{
	};
	public __gc class Derived : public NS::Object
	{
	};
}

int main(void)
{
	NS::Derived __gc* b = new NS::Derived();
	System::Object * p = (b);

	return 0;
}
				

Modification Type:MajorLast Reviewed:4/11/2003
Keywords:kbfix kbbug kbCompiler kberrmsg kbManaged kbpending KB324042