title=call of overloaded FUNCTION_NAME is ambiguous

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Causes[edit | edit source]

Arguments in a function call do not match those in any function declaration[edit | edit source]

For example, a function, "foo", is called from inside main with an argument that is not a perfect match for any of the currently existing implementations of "foo".

void foo(int x);
void foo(double x);

int main () {
   long x = 5000;
   foo(x);
   ...

Solution 1: Cast the argument to match a declaration

int main () {
   long x = 5000;
   foo((int)x);

Solution 2: Create a new, overloaded version of the called function to match the arguments

void foo(int x);
void foo(double x);
void foo(long x);

int main () {
   long x = 5000;
   foo(x);

The same function is defined more than once[edit | edit source]

Solution 1: Look for a misspelled or duplicate function definition/declaration

Solution 2: Make sure you're not using a function name defined in the standard library

template <typename T>
void swap(T &a, T &b) {  // error, "swap" is defined by the C++ standard library
   T tmp = a;
   a = b;
   b = tmp;
}

// Possible Fix
template <typename T>
void Swap(T &a, T &b) {  // Capitalized the first letter
   T tmp = a;
   a = b;
   b = tmp;
}

Notes[edit | edit source]

  • Message found in GCC versions 3.2.3, 4.5.1