Ambiguity

Hello to everyone,
I've got a question: while I was using visual studio for a code it appeared a lot of errors reguarding cout: " cout is ambiguos". So the question is: why?
If I delete using namespace std and then i write it again it disappears but after a while ( and some new code lines) I'm at the same point. How can I solve that problem?
Checking the web I found someone saying that it may depend from the "using namespace std" directive instead of use every time std::cout, which I'd rather to avoid. What does namespace std includes?
Thanks in advance.
Yes, you should avoid using namespace std. There are some things defined in that namespace that have commonly used names, such as max, min, swap, shuffle, list, and set. Writing out std:: every time is preferable and less error prone.

Is it possible you've accidentally defined something somewhere named cout?
you should use only
using namespace
in cpp files .
Is it possible you've accidentally defined something somewhere named cout?

I've checked and I did not find nothing. I've also had that problem with fclose and fseek, then I closed and reopened the project and all errors disappeared.
Helios,if I decided to write std:: everytime, I would need to know what exactly namespace contains in order to know where to do it.

you should use only
using namespace
in cpp files .

I'm already using it, that was what made me curious.
can we see the error(s) that visual studio shows you , completely ?
Helios,if I decided to write std:: everytime, I would need to know what exactly namespace contains in order to know where to do it.
Yes. And? It's not too difficult. Every type, function, or object that's part of the standard library is in the std namespace. This includes names inherited from C, such as std::fopen(), and std::uint32_t. It obviously does not include syntactic elements of the language, such as if, while, sizeof, typeid, decltype, etc.
I would need to know what exactly namespace contains in order to know where to do it.
Everything in standard library. And standard library has a lot of names. If you dump namespace std in global space, you will have a lot of opportunities to run into problems.

You want to name your class plus? Too bad, there already a class with that name. Function measuring distance with the name distance? Well, make sure that call to it will not pick standard library function distance. Forward? Exchange? Already exist. Find, reverse, rotate? It is in here too.

Other namespaces can pose a problem too. using namespace boost and optional? What will happen when optional will be a part of standard library?

I personally against using directive. Using declarations are fine in limited scope when they are needed to make use of ADL (usually affect only a small number of functions). For long namespaces I use namespace aliaces:
1
2
//I would use "time" if not for clashes with C heritage
namespace aeon = std::chrono;

If there are several types I use often, ot they migh change in future, I make type alias:
1
2
3
4
5
using clock_type = aeon::system_clock;
//or even
using clock_type = typename std::conditional_t<aeon::high_resolution_clock::is_steady,
                                               aeon::high_resolution_clock,
                                               aeon::steady_clock>;

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
Last edited on
can we see the error(s) that visual studio shows you , completely ?


The errore that the compiler gives me is:
IntelliSense: "cout" ambiguous in the file main.cpp
IntelliSense errors are not compiler errors. Actually compile your program.
Simply
hold Ctrl + Shift then press F
, choose
entire solution
and type
using namespace std
.

Is there more than one matching lines ?
Topic archived. No new replies allowed.