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.

Null
  • 1,950
  • 9
  • 30
  • 33
Jac08H
  • 91
  • 4
  • 14
    We need to declare all-out war on `using namespace std;`. – Fred Larson Jul 23 '15 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) – Lilith Daemon Jul 23 '15 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 '15 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". – Lilith Daemon Jul 23 '15 at 17:01

2 Answers2

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.

yizzlez
  • 8,757
  • 4
  • 29
  • 44
  • 4
    @Jac08H Read http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Jeff S Jul 23 '15 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 '15 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. – Laurent LA RIZZA Sep 13 '17 at 07: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.
Null
  • 1,950
  • 9
  • 30
  • 33