function error

i always get an error
error : reference to 'plus' is ambiguos

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
33
34
int plus(int a,int b)
{
    return a+b;
}
int divide(int a,int b)
{
    if(b==0)
    {
        cout<<"cannot divide by 0";
    }
    else
    {
        return a/b;
    }
}
int times(int a,int b)
{ 
    return a+b;
}
int minus(int a,int b)
{
    return a-b;
}
int main()
{
   int a;
   int b;
   cout<<"Enter two integers ";
   cin>>a>>b;
   plus(a,b);      //error : reference to 'plus' is ambiguos
   divide(a,b);
   times(a,b);
   minus(a,b);
}

what should i do
See NT3's response in your other thread.
http://www.cplusplus.com/forum/beginner/135351/
Hmmm... Try making a variable in the function "plus". Make it an integer and name it something random... like "x". Set x to equal a + b, and then have the function return it. Like this:

1
2
3
4
int plus(int a, int b){
      int x = a + b;
      return x;
}


I would try that.

PS: I don't know if it is a typo, but the function times returns a PLUS b, not a times b.
You didn't show all of your code. However it looks as though you have
 
    using namespace std;
at the start of your program.

The compiler can't decide whether you meant to use your local function named plus, or std::plus in the standard library.

To avoid this, instead of using namespace std; either declare only those individual items you want to use, such as
 
    using std::cout;


or add the prefix in front of each use, such as
 
    std::cin>>a>>b;


An alternative is to prefix the use of your function with :: to indicate that you mean the locally defined version.
 
    ::plus(a,b);
though that is perhaps not such a good solution in this case.
I think its because your code has + operator for plus function and times, you want * not + for times.
this should work....also store your return values to variables
------------------------
#include<iostream>
using namespace std;


int plus(int a,int b)
{
return a+b;
}
int divide(int a,int b)
{
if(b==0)
{
cout<<"cannot divide by 0";
}
else
{
return a/b;
}
}
int times(int a,int b)
{
return a*b;
}
int minus(int a,int b)
{
return a-b;
}
int main()
{
int a;
int b;

cout<<"Enter first integer ";
cin>>a;
cout<<"Enter second integer ";
cin>>b;

cout << "Sum: " << plus(a,b) << endl;
cout << "Quotient: " << divide(a,b) << endl;
cout << "Product: " << times(a,b) << endl;
cout << "Difference: " << minus(a,b) << endl;
}
i see. its clearer now >< thank you all
Topic archived. No new replies allowed.