3

I was going through the Namespace topics and found that when we compile the below code the compiler complaints

[Error] reference to 'cout' is ambiguous

#include <iostream> 

void cout() 
{
 std::cout << "In cout function\n";
}

int main()
{
using namespace std; 
cout << "Hello, world!\n";
return 0;
}

But if we replace using namespace std with using std::cout, the program runs successfully and produces the expected output. How is this possible?

#include <iostream> 

int cout() 
{
std::cout << "In cout function\n";
}

int main()
{
using std::cout;  //REPLACED 
cout << "Hello, world!\n";
return 0;
}

As far as I am aware the only difference between using std::cout and using namespace std is,

using std::cout -> imports only cout from std namespace, while

using namespace std -> imports all the variables from std.

In both of the cases cout() function and std::cout variable are visible within main function. So how is using std::cout resolving the issue while using namespace std is not able to?

Evg
  • 25,259
  • 5
  • 41
  • 83
Akshay J R
  • 111
  • 4
  • 5
    Neither is an import. I'm afraid experience in other languages is a disadvantage here, because C++ is very peculiar in this regard. See [Using directive vs using declaration](https://stackoverflow.com/q/48356856/817643) for example. – StoryTeller - Unslander Monica Mar 26 '23 at 12:59
  • 1
    You can't learn c++ by trial and error. It must be learnt using a [good c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Read about using directive and using declaration. – Jason Mar 26 '23 at 13:00
  • 1
    Also see dupe: [About the ambiguity of using a name vs using a namespace when doing unqualified calls](https://stackoverflow.com/questions/71690024/about-the-ambiguity-of-using-a-name-vs-using-a-namespace-when-doing-unqualified) where this is explained in very detail. – Jason Mar 26 '23 at 13:02
  • 2
    @spectras possibly because first related question is immediately "https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?rq=2" ... and one of first reasons is getting compiling errors about ambiguity. – Öö Tiib Mar 26 '23 at 13:14
  • 2
    Duplicate of [About the ambiguity of using a name vs using a namespace when doing unqualified calls](https://stackoverflow.com/questions/71690024/about-the-ambiguity-of-using-a-name-vs-using-a-namespace-when-doing-unqualified) and [Using directive vs using declaration](https://stackoverflow.com/q/48356856/817643) – Kal Mar 26 '23 at 13:35
  • 1
    Does this answer your question? [About the ambiguity of using a name vs using a namespace when doing unqualified calls](https://stackoverflow.com/questions/71690024/about-the-ambiguity-of-using-a-name-vs-using-a-namespace-when-doing-unqualified) – Kal Mar 26 '23 at 15:28

1 Answers1

3

Informally:

using std::cout means "in this scope, the identifier cout refers specifically to std::cout, so don't go looking anywhere else for it".

using namespace std means "add all the names in std to the current scope, in addition to everything else that is also in scope".

In the first case, you have explicitly said that cout means std::cout– the global cout is hidden and there can be no ambiguity.

In the second case, cout is ambiguous since it can refer to either std::cout or your function.

You will see similar errors in the first case if you try to declare your function inside main:

int main()
{
    int cout();
    using std::cout;
    cout << "Hello, world!\n";
    return 0;
}
molbdnilo
  • 64,751
  • 3
  • 43
  • 82