0

I have read about the "cout/cin is ambiguous" plenty of times by now, from many different kinds of sources, and I keep hearing:

"Use 'std::' as a prefix, since it elsewise might confuse your compiler"

Now, I do know that not using the scope operator can be a bad practise according to some, and that people who usually receives this sort of error is on an elementary stage in their programming. However, the sources says that I'm supposed to understand how this work with time, which unfortunately, I don't yet. I still receive the errors occasionally (since I, despite all the warnings, still use "using namespace" declarations), and I still don't get why it works in 99% or the cases, and not in the other.

I'm degugging my code in Visual Studio express (2012), and of course, all the required sources are included (which in this case is the iostream library), so there isn't any sort of permanent error; it actually tends to popup quite randomly (a couple of times for instance, I've changed a piece of code, making the error appear, then even after changing it back, the error persists)... Really, I don't see any logical explaination to why the error appears in random contexts - hence why I'm asking:

What is causing this error, exactly (what does it mean, and why and how does it confuse the processor)? Moreover, is it worth skipping these declarations because of that? Can't it be fixed somehow?

Thanks in beforehand!

Max
  • 897
  • 1
  • 10
  • 27
  • 2
    Please provide an SCCE showing how the `using namespace` declaration doesn't work in those 10% of cases – John Dibling Jan 15 '14 at 21:56
  • What error? Paste it, and if possible, the code that generated it. – TypeIA Jan 15 '14 at 21:56
  • @John Dibling (SCCE? Now I'm assumming that that's a regular code-example, so please correct me if I'm wrong) Actually, I regret putting 10% there, since it occurs way less (when it appears though, it still looks like a normal code). I'll try a few times, and if I see that the error appears, I'll put it as an example. – Max Jan 15 '14 at 22:00
  • @dvnrrs I will, but as said above, it's hard to get the error just like that. However, it returns: " is ambiguous" (so it applies for all std objects). – Max Jan 15 '14 at 22:01
  • 1
    I suggest you wait until the next time it happens and post a question about it then. – Steve Jessop Jan 15 '14 at 22:03
  • @Steve Jessop Note that this isn't a huge problem for me personally, and it's not as if I can't bypass it or so. I'm mainly asking this for the sake of curiosity - and to hopefully learn a thing or two (basically, I want answers to the last three questions, which in my opinion, doesn't need code examples. I **don't** need help with a specific code, to make it work, even though I'll add that later if possible, as something extra) – Max Jan 15 '14 at 22:04
  • 4
    More often than not, it is not the compiler but the user that gets confused... – David Rodríguez - dribeas Jan 15 '14 at 22:05
  • @Captain Obvlious I don't use more than one namespace, if that's what you're saying. – Max Jan 15 '14 at 22:06
  • You should provide an example where you get the error, or if you cannot reduce the issue to a minimal test case, you should at the very least copy the exact error message that you are getting. – David Rodríguez - dribeas Jan 15 '14 at 22:06
  • 1
    See here http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – juanchopanza Jan 15 '14 at 22:08
  • @Max: the problem is that nobody knows what error you might be talking about, so we're hardly going to be able to tell you what causes it. And if you remove your unreliable error from the question, all that's left is a duplicate of a previous question. Simply, `using namespace std;` *does not* "confuse" the compiler. I don't know what it is doing in your case because you can't describe the problem, but confusing the compiler isn't it. – Steve Jessop Jan 15 '14 at 22:09
  • @David Rodríguez - dribeas Hmm, isn't it possible to answer the question: "what does it mean, and why and how does it confuse the processor" without that? I've seen other people who have had correct code, yet this error. And as said, I'm not having issues with a specific code; rather than being interested in this matter. – Max Jan 15 '14 at 22:09
  • The answers to the general question are in the link I posted earlier. – juanchopanza Jan 15 '14 at 22:12
  • The real problem you face if you write using namespace inside your headers – Sebastian Hoffmann Jan 15 '14 at 22:17
  • 1
    @Max: Don't take me wrong, lookup is complicated, it is far more complicated than most people will even realize. But the rules are precisely stated in the standard and compilers implement them literally (excluding bugs and *extensions*). When you talk about the compiler being confused you probably mean *ambiguity* errors (this is why it is important to post the real error!), and the compiler is not confused, it is applying the rules and reaching the conclusion that the standard mandates. – David Rodríguez - dribeas Jan 15 '14 at 22:31
  • BTW, you could also provide a link where you have read that *Use 'std::' as a prefix, since it elsewise might confuse your compiler*. I have used a similar sentence many times when trying to get people to qualify the identifiers, but substituting *compiler* with *maintainer*. It is clear when looking at code that `std::find` refers to the algorithm, while `find` might be a member function, a free function in your namespace or anything else, it is just too common a word. The compiler will apply the lookup rules and determine which one it is, but your coworker won't do that. – David Rodríguez - dribeas Jan 15 '14 at 22:43
  • @David Rodríguez - dribeas I don't remember where I found that exactly, but I've seen it several times, at different places. And, I do understand how it works when it comes to different namespace declarations and usages (as you explained), but what doesn't get clear to me is how that can happen with `cout`, especially not in a program where I haven't created any custom namespace, nor included any file other than `iostream` and `string`. I'm trying my best to show you what I mean, but it's hard to make it occur since I don't know the cause yet. However, as soon as I see it again, I'll add that. – Max Jan 20 '14 at 19:16
  • @Max: Maybe you misunderstood the quote. Whenever I have heard something alike it was always for the clarity of the user, not the compiler. I suggest that you remove the question until you find the appropriate quote and then reformulate it. – David Rodríguez - dribeas Jan 20 '14 at 22:47

1 Answers1

1

This is how you can confuse it by doing using namespace ... religiously. Now suppose some library you are using has defined a function called foo. Let's assume it's boost or whatever. Maybe even your own namespace called "op". You have a function called foo. My namespace may have the same function signature as yours. If you import both namespaces, how will the compiler know which to call? See below for example.

#include <cstdio>

namespace me
{
    void foo()
    {
        printf("%s", "Calling me::foo\n");
    }
}

namespace op
{
    void foo()
    {
        printf("%s", "Calling op::foo\n");
    }
}


using namespace me;
using namespace op;

int main()
{
    foo(); //abiguous call.. Which foo is it going to use? op::foo? Or me::foo?

    //now remove the using namespace me and using namespace op.. then try the below:

    me::foo();
    op::foo();

    //The compiler now knows which foo you're trying to call and calls the correct one.
}
Brandon
  • 22,723
  • 11
  • 93
  • 186
  • Thanks a lot for the reply, although in my case, it occurs with regular members of the std namespace, without any other namespaces being used/included. Sometimes, all the different members are even gives the "abiguous"-error at the same time, which doesn't make any sense in my opinion (and trust me, I don't add anything that can disturb it). But still - if that's all that there were to "confusing the compiler", then it wasn't as complicated as I expected it to be. – Max Jan 20 '14 at 19:21
  • You don't need to have a namespace yourself. Imagine you have a global function called foo and some namespace (for example `std`) has one called foo with the same signature, that can also cause ambiguity. – Brandon Jan 20 '14 at 22:08