Export (0) Print
Expand All
Expand Minimize

Compiler Error C2666

'identifier' : number overloads have similar conversions

An overloaded function or operator is ambiguous.

Possible cause

  • Formal parameter lists are too similar to resolve ambiguity.

Possible solution

  • Explicitly cast one or more of the actual parameters.

The following sample generates C2666:

// C2666.cpp
struct complex
{
   complex(double);
};

void h(int,complex);
void h(double, double);

int main()
{
   h(3,4);   // C2666
}

This error can also be generated as a result of compiler conformance work that was done for Visual Studio .NET 2003: binary operators and user-defined conversions to pointer types.

For the binary operators <, >, <=, and >=, a passed parameter is now implicitly converted to the type of the operand if the parameter's type defines a user-defined conversion operator to convert to the type of the operand. There is now potential for ambiguity.

See Summary of Compile-Time Breaking Changes for more information.

For code that is valid in both the Visual Studio .NET 2003 and Visual Studio .NET versions of Visual C++, call the class operator explicitly using function syntax.

// C2666b.cpp
#include <string.h>
#include <stdio.h>
struct T
{
   T( const T& copy )
   {
      m_str = copy.m_str;
   }

   T( const char* str )
   {
      m_str = new char[strlen( str )+1];
      strcpy( m_str, str );
   }

   bool operator<( const T& RHS )
   {
      return m_str < RHS.m_str;
   }

   operator char*() const
   {
      return m_str;
   }

   char* m_str;
};

int main()
{
   T str1( "ABCD" );
   const char* str2 = "DEFG";

   // Error – Ambiguous call to operator<()
   // Trying to convert str1 to char* and then call 
   // operator<( const char*, const char* )?
   //  OR
   // trying to convert str2 to T and then call
   // T::operator<( const T& )?

   if( str1 < str2 )   // C2666
   {
      printf("str1 < str2 \n");
   }
   // Try one of the following statements instead
   if ( str1.operator < ( str2 ) )   // Treat str2 as type T
   {
      printf("str1.operator < ( str2 )\n");
   }

   if ( str1.operator char*() < str2 )   // Treat str1 as type char*
   {
      printf("str1.operator char*() < str2\n");
   }
}
Show:
© 2014 Microsoft