8

I am trying to write a function which takes two numbers and prints out their sum.

#include <iostream>

using namespace std;

int plus(int, int);

int main () {
 int a, b, result;
 cout << "2 numbrs";
 cin>>a>>b;
 result = plus(a,b);
 cout << result;
 return 0;
}

int plus(int a,int b) {
 int sum;
 sum = a+b;
 return sum;
}

and error I get:

use of `plus' is ambiguous

It´s my first C++ program and in fact I am getting blind finding an error.

4
  • 14
    We need to declare all-out war on using namespace std;. Jul 23, 2015 at 16:55
  • 3
    @FredLarson Is curious how almost all introductory programming books/tutorials employ using namespace std; without properly explaining what that means, and when you should actually use it. (Most of the time you shouldn't) Jul 23, 2015 at 16:58
  • In fact I read good explanation what is happening when using using namespace std, but I didn´t realize that can cause this problem.
    – Jac08H
    Jul 23, 2015 at 16:59
  • 9
    @Jac08H Well that is kinda my point... If you read an "explanation" and it doesn't explain why it is a problem, then it really isn't a "good explanation". Jul 23, 2015 at 17:01

2 Answers 2

21

Either do

result = ::plus(a,b);

Or rename the function. This is a good lesson on why using namespace std is not considered good practice.

3
  • 4
    @Jac08H Read stackoverflow.com/questions/1452721/…
    – Jeff S
    Jul 23, 2015 at 16:56
  • 2
    @Jac08H There's no need to marked your question as fixed. You can just accept the appropriate answer.
    – yizzlez
    Jul 23, 2015 at 17:19
  • using namespace std is considered bad practice only in header files, and it's OK in implementation files. (because it does not ooze into other modules) using namespace std is OK here. The real problem here is that the plus function is uselessly reinventing functionality, and uses a common word to do so, and this word happens to already be loaded with semantics by the Standard Library. I'd advocate to either directly use operator + instead of defining a function, or to define the function in the project's namespace. Sep 13, 2017 at 7:21
15

There is already a function object in the std namespace called plus. Because of using namespace std; this std::plus is put in the global namespace, which is also where your plus() is named. When you attempt to call your plus() the compiler can't tell whether you are referring to std::plus or your plus() because they are both in the global namespace.

You have the following options:

  1. Remove using namespace std; (you'll then need to qualify other functions in the std namespace -- e.g. std::cout).
  2. Put your plus() in its own namespace (say, mine), and call it using mine::plus(a, b).
  3. Call your function with ::plus() as suggested (assuming you don't put it in its own namespace).
  4. Rename the function so that there is no name collision.
2
  • std::plus is not a function, it's a function object. If you want to call it, you need to do std::plus()(a,b) or std::plus{}(a,b). Sep 13, 2017 at 7:24
  • @LaurentLARIZZA Fixed. Thanks.
    – Null
    Sep 13, 2017 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.