c++ overload 3 function with different parameters(int,*int,&int)

user3134909

So i have this homework to do "Using functions overloading define 3 functions with the same name but with different prams type (int, int*, int&) that will return the square root of the value." Well i made it but i don't know it gives me this error:"ambiguous call to overloaded function". I try to fix it but no succes... Here's my code it's quite simple:

#define _CRT_SECURE_NO_WARNINGS
#include <math.h>
#include <iostream>
using namespace std;

double rad(int);
double rad(int*);
double rad(int&);

int main(){
int a,*pt=&a;
cin>>a;
cout<<"Radical din "<<a<<" este "<<rad(a)<<endl;
cout<<"Radical din "<<a<<" este "<<rad(pt)<<endl;
cout<<"Radical din "<<a<<" este "<<rad(&a)<<endl;
return 0;
}

double rad(int x){
    return  (sqrt(x));
}
double rad(int *x){
    return  (sqrt(*x));
}
double rad(int &x){
    return  (sqrt(x));
}
Barry

Given these declarations:

double rad(int);   // (1)
double rad(int*);  // (2)
double rad(int&);  // (3)

Dealing with (2) is easy. We either have a pointer, or we don't, there's no possible confusion. The question then is, what happens with:

int a;
rad(a);

Well, here both (1) and (3) work. And there's actually no preference between the two anywhere in the rules for determining what the best viable candidate is! It's an ambiguous call, so this will always fail to compile.


Note that it is possible to explicitly call (1) though - by simply passing in an int that you can't take a reference to:

rad(4); // calls (1), since neither (2) nor (3) are possible

Note also that it is possible to call (1) or (3) even with a named variable like a, but we can't rely upon overload resolution to do it. We have to explicitly tell the compiler which function we want. And to do that, we have to cast the name:

int a;
static_cast<double(*)(int)>(rad)(a);  // calls (1) explicitly
static_cast<double(*)(int&)>(rad)(a); // calls (3) explicitly

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to overload function for different iterator value_types in C++

From Dev

Overload operator for int and char in c++

From Dev

C++ function overload priority

From Dev

Python - overload same function with different arguments

From Dev

Operator= Overload with different parameter in C++?

From Dev

C++ pass by value / by reference function overload

From Dev

C++ function template overload on template parameter

From Dev

How to use a overload function in a constructor in c#

From Dev

C++ template function overload resolution bug

From Dev

Function not found from global namespace if there's an overload with different argument types

From Dev

pass Int to C function

From Dev

Where to overload division returning custom class C# operator/ (int,int)

From Dev

C assign an int to a function pointer

From Dev

C++11 std::function const overload ambiguity

From Dev

C++ Overload Resolution, userdefined conversion and function template

From Dev

C++ template specialization: unexpected function overload lookup result

From Dev

Is a c++11 variadic function template overload with a dependent type ambiguous?

From Dev

Weird overload function situation with QT/C++ types

From Dev

c++ postfix / prefix operator overload as non-member function

From Dev

C++ gcc function template overload compiler issue

From Dev

C++11 std::function const overload ambiguity

From Dev

C++: Overload a member's constructor function with keyword operator

From Dev

overload resolution from magic number to int or long (in range-v3)

From Dev

overload resolution from magic number to int or long (in range-v3)

From Dev

Overload addAll function in Java

From Dev

Overload resolution with std::function

From Dev

Overload resolution for pointer to function

From Java

Overload a lambda function

From Dev

Template function overload resolution

Related Related

  1. 1

    How to overload function for different iterator value_types in C++

  2. 2

    Overload operator for int and char in c++

  3. 3

    C++ function overload priority

  4. 4

    Python - overload same function with different arguments

  5. 5

    Operator= Overload with different parameter in C++?

  6. 6

    C++ pass by value / by reference function overload

  7. 7

    C++ function template overload on template parameter

  8. 8

    How to use a overload function in a constructor in c#

  9. 9

    C++ template function overload resolution bug

  10. 10

    Function not found from global namespace if there's an overload with different argument types

  11. 11

    pass Int to C function

  12. 12

    Where to overload division returning custom class C# operator/ (int,int)

  13. 13

    C assign an int to a function pointer

  14. 14

    C++11 std::function const overload ambiguity

  15. 15

    C++ Overload Resolution, userdefined conversion and function template

  16. 16

    C++ template specialization: unexpected function overload lookup result

  17. 17

    Is a c++11 variadic function template overload with a dependent type ambiguous?

  18. 18

    Weird overload function situation with QT/C++ types

  19. 19

    c++ postfix / prefix operator overload as non-member function

  20. 20

    C++ gcc function template overload compiler issue

  21. 21

    C++11 std::function const overload ambiguity

  22. 22

    C++: Overload a member's constructor function with keyword operator

  23. 23

    overload resolution from magic number to int or long (in range-v3)

  24. 24

    overload resolution from magic number to int or long (in range-v3)

  25. 25

    Overload addAll function in Java

  26. 26

    Overload resolution with std::function

  27. 27

    Overload resolution for pointer to function

  28. 28

    Overload a lambda function

  29. 29

    Template function overload resolution

HotTag

Archive