ambiguous call to overloaded function

Sep 13, 2020 at 8:06pm
Hello, I have created a function, but I am getting two errors, Ambiguous call to overloaded function and more than one instance of overloaded function matches the argument list. When I was learning what ambiguity was, I learned that it was when the compiler could not differentiate between two functions with the same name. But in my program I only have the one function. Can there be another reason for this? I have my code below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include <iostream>
#include <cmath>

using namespace std;

void round(double &num);

int main()
{
	double number;	
	cout << "Enter a number : \n";
	cin >> number;

	round(number);
}

void round(double &num)
{
	double x;
	double roundedNum;
	x = modf(num,  &roundedNum);	
	cout << x;
}
Sep 13, 2020 at 8:17pm
There is a function called "round" in <cmath> - you've managed to hit that.
Removing "using namespace std;" won't make any difference.

I'd just call your function something different. Make yourself popular in the bar and call it "my_round".
Last edited on Sep 13, 2020 at 8:24pm
Sep 13, 2020 at 8:26pm
There is a function called "round" in <cmath>


Ah thank you. That is something I should have thought of, still getting stuck on these stupid mistakes :( .
Sep 13, 2020 at 8:55pm
The method above is probably the best way for this program, but don't forget you could also put your function into your own "custom" namespace.

Sep 13, 2020 at 8:59pm
you could also put your function into your own "custom" namespace.


I have read about custom namespaces before but not covered a lot about it. I am currently following a book hoping i will learn programming this time (have failed to stick with it many times in the past) so i think namespaces will come up again soon. So I will try your suggestion when I cover namespaces and learn more about what they actually are.
Sep 13, 2020 at 9:17pm
For your code:
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
#include <iostream>
#include <cmath>
using namespace std;

namespace my
{
   void round(double &num);
}

int main()
{
	double number;	
	cout << "Enter a number : \n";
	cin >> number;

	my::round(number);
}

void my::round(double &num)
{
	double x;
	double roundedNum;
	x = modf(num,  &roundedNum);	
	cout << x;
}



or declared/defined at the same time:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>
using namespace std;

namespace my
{
   void round(double &num)
   {
      double x;
      double roundedNum;
      x = modf(num,  &roundedNum);	
      cout << x;
   }
}

int main()
{
	double number;	
	cout << "Enter a number : \n";
	cin >> number;

	my::round(number);
}
Last edited on Sep 13, 2020 at 9:22pm
Sep 13, 2020 at 9:28pm
> I learned that it was when the compiler could not differentiate between two
> functions with the same name. But in my program I only have the one function.
foo.cpp:14:14: error: call of overloaded ‘round(double&)’ is ambiguous
/usr/include/bits/mathcalls.h:301:1: note: candidate: ‘double round(double)’
foo.cpp:6:6: note: candidate: ‘void round(double&)’

read the complete error message
¿don't understand the message? then post it and we'll explain it
Sep 13, 2020 at 9:44pm
Also to get you going in the right direction with namespaces I recommend you stop using the "using" clauses and instead use the scope resolution operator:: to scope std functions and classes, ie: std::cin.

Topic archived. No new replies allowed.