E2015 Ambiguity between 'function1' and 'function2' (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Compiler Errors And Warnings (C++) Index

The compiler cannot distinguish what function is called. Possible causes and solutions are discussed here.

Ambiguous Overloaded Functions

If the functions are overloaded, and more than one function definition can be used with the supplied parameters, this error is emitted. To resolve the ambiguity, modify the type of the parameters so that the compiler can identify what function is called.

Example:

void foo(short); // #1
void foo(char);  // #2
int I;
foo(i);          // error: ambiguous
foo((char)i);    // picks #2

Ambiguous Constructors or Conversion Operators

One possible solution is to make the constructor and/or operator explicit using the keyword explicit.

Example:

struct A {
   A(const A&);
   A(const wchar_t*);
};
struct B {
   operator A();
   explicit  operator wchar_t*();
};  
int main() {
   B v;
   A s(v); // ok (since operator wchar is explicit)
}

Ambiguous Namespace or Unit Scope Names

Ambiguity results if the functions are declared in different namespaces that are used simultaneously. To resolve the ambiguity, specify the namespace of the function to be called. (See the scope resolution operator and Unit Scope Names.)

Example:

struct A { void foo(); }; // #1 
struct B { void foo(); }; // #2
struct C : A, B  {
  void callfoo() {
      foo(); //error:  ambiguous
      B::foo(); // picks #2
    }
};

Example:

[BCC32 Error] Unit4.cpp(19): E2015 Ambiguity between '_fastcall Fmx::Printer::Printer()' and 'Fmx::Printer'

This is "As Designed" because there is both a namespace and a function named Printer in scope. To fix the source, you should write:

	void __fastcall TForm4::FormCreate(TObject *Sender)
	{ 
		Fmx::Printer::Printer()->BeginDoc();
	}

Example:

	namespace Ns {
		namespace Foo {
			void Foo() {}
		}
	}

	Using namespace Ns::Foo;
	Using namespace Ns;

	void Bar {
		Foo();	// error!  Which Foo?
				// should instead be Ns::Foo::Foo();
	}

See Also