This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Help : Is this a namespace collision ??


On Mon, Nov 25, 2002 at 06:55:52AM +0100, Christian POKAM wrote:
>  Why is the Compilation of NewHandler.cpp complaining about  
>      the ambigous use of "count" ??
>  Thank you 

It's complaining because you asked it to.
 
> [...]
> using namespace std;
> int count = 0;
> NewHandler.cpp:16: use of `count' is ambiguous
> NewHandler.cpp:12:   first declared as `int count' here
> /usr/include/c++/3.2.1/bits/stl_algo.h:390:   also declared as `
>    std::iterator_traits<_Iterator>::difference_type
> std::count(_InputIter,
>    _InputIter, const _Tp&)' here

You asked to inherit all the names from std::, including std::count,
into your global namespace.  Then, you declared another name ::count
there.  That's allowed, but then when you use the name, the compiler
doesn't know which one you mean.  You have to say "::count" in the
code example you gave.

This is why "using namespace std;" is always a bad idea, despite that
book authors use it frequently just to save on space in their examples.
Since they typically don't write real programs any more, they often
don't understand what namespaces are for.  It's much better to fully
qualify standard names wherever you use them in your program (e.g.
"std::cout << std::endl;", or (if you can't bear to do that) to use 
namespace-declarations like "using std::cout;" for those standard 
names you use a lot.

Be aware that the next standard library will use lots more short names,
so that even if you get lucky and don't collide with any now, with a 
future version you may not be so lucky.

Nathan Myers
ncm-nospam@cantrip.org


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]