0
#include <iostream>

using namespace std;

int dig()
{
    return 5;
}


int num2()
{
    return 9;
}

int num_get()
{   

    return dig()+num2(); 
}
int main()
{
    cout<<num_get();
}

I am just a beginner who was practicing some c++ code.when I was trying to call num_get() function. The compiler says the following:-

>error: reference to ‘num_get’ is ambiguous
   23 |     cout<<num_get();
      |           ^~~~~~~
In file included from /usr/include/c++/9/ios:41,
                 from /usr/include/c++/9/ostream:38,
             from /usr/include/c++/9/iostream:39,
                 from main.cpp:1:
/usr/include/c++/9/bits/localefwd.h:157:11: note: candidates are: ‘template class 
   std::num_get’
      157 |     class num_get;
          |           ^~~~~~~
    main.cpp:16:5: note:                 ‘int num_get()’
       16 | int num_get()
          |     ^~~~~~~

I was able to resolve the error by changing the value of num_get() to numb_get."What I want to know is simple just tell me what is wrong with num_get() function with clear information." sorry if I was annoying.I searched about the error in internet didn't get relevant info that's why i posted this question.

Thanks for spending your valuable time answering my beginner question hope i will ask some good question next time.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
bruce
  • 11
  • 4
    https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – πάντα ῥεῖ Apr 24 '22 at 16:21
  • 4
    due to `using namespace std` the compiler doesn't know if you want your `num_get` or [`std::num_get`](https://en.cppreference.com/w/cpp/locale/num_get) – Alan Birtles Apr 24 '22 at 16:28
  • You simply should not use `using namespace std;` unless you're sure you'll have no conflicts. – πάντα ῥεῖ Apr 24 '22 at 16:28
  • The key is here: `note: candidates are: ‘template class std::num_get’` – Dave Newton Apr 24 '22 at 16:31
  • @πάνταῥεῖ even if you can be sure that there's no conflict now, you can't be sure that it won't be conflicted in the future. C++ constantly adds new features and people also need to add new libraries so a name can clash unexpectedly. In short never use that and only `using std::cout`, `using std::cin`... – phuclv Apr 24 '22 at 16:32
  • @πάντα ῥεῖ I'd go further. You shouldn't use it at all. The standards committee kept the name of the `std::` namespace short for a reason. – Paul Sanders Apr 24 '22 at 16:37
  • 1
    You can add to the list of "what's going on here?" `std::gcd`, `std::data`, `std::swap`, and probably more. All of those `using namespace std;` code that have `gcd`, `data`, `swap` functions and names will in many aspects, fail to compile, or compile but have behavior different than expected. – PaulMcKenzie Apr 24 '22 at 16:45

0 Answers0