Why ambiguous? (function overloading)

I cannot understand why inside my main() the function myfunct(i) is ambiguous? I got the code from a book. I run the code(to some free IDE) and the error i get says: ambiguous.
The book explains it: inside the main(), 'i' value , if it converts to an other data type (char, long double,float,long int) it will fit with more than one versions of the same function. Huh??Why? I thought it was possible this:
int i ---convert to---> long int...

Rules of overloading(brielfy, not everything):
possible 'upgrades':
-unsigned char,short int,bool 'upgrade' to int.
- float 'upgrades' to double.
data type conversions follow up.(if 'upgrades' cannot help enough).Eg int->long int,int->float,double->long double, double->bool and more....

i looked wiki, but in vain.... :-(

rules of function overloading: If you could enlighten me with a good tutorial,to the point( not too much bla bla) and easy to grasp it, i would appreciate it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

#include <iostream>
#include <cstdlib>
void myfunct(long double a);
void myfunct(long int a);
void myfunct(float a);
void myfunct(char a);

using namespace std;

int main(int argc, char  *argv[])
{
    int i=8;
    //myfunct(i);
    cout << myfunct(i);
    cout << "\n\n";
    system("pause");
}

// the following code is not in the book, i added it now.
void myfunct(long double x){
     cout << x;
}
void myfunct(long int x){
     cout << x;
}
void myfunct(float x){
     cout << x;
}
void myfunct(char x){
     cout << x;
}



Thanks...
Last edited on
well, you should reject that book.
Since you haven't defined or implemented a function overload that takes an int parameter how is the compiler supposed to know what function to call, since the int can be safely promoted to one of the three other overloaded types. It shouldn't try to call the char overloaded function because of possible loss of precision.

Also note that you can't print a "void" return.

Get a good book. I am sick of posting that link, so just google "stack overflow C++ book"
This forum needs signatures...
Last edited on


@mynicks,
http://www.cplusplus.com/forum/beginner/152503/

"Programming: Principles and Practice using C++" is harassing to beginners. i.e.
http://www.cplusplus.com/forum/beginner/152765/
Well overloading needs to know what type a variable is. If there is no function of the same type as the variable it can check to see if it can do an implicit conversion, but your code gives it multiple choices for conversion so it doesn't know which one to choose. There is a hierarchy of types, but this is used during calculations (since only values of the same type can be calculated, it will convert the lesser type to the higher type then calculate them), and isn't used to pick one implicit conversion over another when multiple type could be valid.

The number one thing about overloading is for the program to know which type to go with. You should use an explicit type conversion.

If you really want the same variable at the same point in the program to be able to go to a number of different possible type functions, then you can use an if chain based on whatever factor is used to make the choice of which type, calling the function with an explicit conversion in each.

Or if you need it to go through all of them, simply run them one after another with explicit conversion each time.
Topic archived. No new replies allowed.