Topic: [Q]: Add "using namespace;" to avoid name scope clashes between for exemple <fstream> and <strstream.h>
Author: Tim Van Holder <tim.vanholder@falconsoft.be>
Date: 18 Oct 2001 00:01:04 GMT Raw View
Francis ANDRE wrote:
>
> For exemple
>
> #include <fstream>
> #include <vector>
> using namespace std:
> class foo {
> public:
> vector<int> bar1;
> vector<int> bar2;
> friend ostream& operator << (ostream& os, const foo&);
> };
> ....
> ....
> #include <strstream.h>
The whole point of namespaces is to avoid these clashes.
Of course, if you use a `using' directive before including a file that
contains declarations in the global namespace, you're asking for
trouble.
So either group includes at the top of the file, with any and all
`using' directives somewhere after them, or, if the above example is
what's going wrong, use std::stringstream instead of strstream.
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: belvis@pacbell.net (Bob Bell)
Date: 19 Oct 2001 14:57:57 GMT Raw View
> In my opinion, the -- using namespace;-- could be a easy solution both for
> compiler and integrator
> to cope with the problem.
It seems to me that an even easier solution is to not use the "using
namespace std;" directive; at most, you can use using declarations to
get fine-grained control of what you want. Where that breaks down, use
fully qualified names:
#include <fstream>
#include <vector>
using std::vector;
class foo {
public:
vector<int> bar1;
vector<int> bar2;
friend std::ostream& operator << (std::ostream& os, const foo&);
};
....
....
#include <strstream.h>
....
....
No muss, no fuss.
Bob
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Robert O'Dowd" <nospam@nonexistant.com>
Date: 19 Oct 2001 17:05:37 GMT Raw View
Francis ANDRE wrote:
>
> Hi DG
>
> Does the C++ standard or Request for enhancement list plan to add:
> using namespace;
>
> This will be helpful in avoiding name scope clashes when using a "new" std
> namespace and "old" global namespace.
>
> For exemple
>
> #include <fstream>
> #include <vector>
> using namespace std:
> class foo {
> public:
> vector<int> bar1;
> vector<int> bar2;
> friend ostream& operator << (ostream& os, const foo&);
> };
> ....
> ....
> #include <strstream.h>
Yuck. You've used the final (as standardised) version of
the headers for vector and fstream, and an older deprecated
form for strstream.
Why not just use #include <strstream> ?
Also, it is generally good style to #include standard headers
before you make any declarations of your own functions or
types. Such an approach would also help in this situation.
> ....
> ....
>
> At this point, the compiler emits an error claiming that "streambuf" and
> "ostream" referenced in the definition of strstream is ambigous because the
> declaration of "class streambuf;" conflicts with "typedef
> basic_streambuf <char, char_traits<char> >" from <fstream>.
>
> By inserting a reset command like -- using namespace; -- of the used
> namespace before the including of <strstream.h>,
> one can avoid this kind of problem.
>
> I know that one cannot intermix <iostream> and <iostream.h> but when an
> application is
> using one library with <iostream.h> and another one with <iostream> comming
> from 2 differents
> providers, this is a real painful problem!!!
A better solution, IMHO, is to update the code so it simply uses the
new style headers, and the (presumably) more recent library.
I've yet to see code where it makes sense to #include both iostream
and iostream.h within one source file. Given that functions in the
new style headers generally live in namespace std (or, in the
case of functions like operator<<) take arguments of type that
are declared in namespace std, this problem should not affect the
linking of your program (eg a.cc uses <iostream.h> and b.cc uses
<iostream>).
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: "Francis ANDRE" <francis.andre@easynet.fr>
Date: 16 Oct 2001 18:27:13 GMT Raw View
Hi DG
Does the C++ standard or Request for enhancement list plan to add:
using namespace;
This will be helpful in avoiding name scope clashes when using a "new" std
namespace and "old" global namespace.
For exemple
#include <fstream>
#include <vector>
using namespace std:
class foo {
public:
vector<int> bar1;
vector<int> bar2;
friend ostream& operator << (ostream& os, const foo&);
};
....
....
#include <strstream.h>
....
....
At this point, the compiler emits an error claiming that "streambuf" and
"ostream" referenced in the definition of strstream is ambigous because the
declaration of "class streambuf;" conflicts with "typedef
basic_streambuf <char, char_traits<char> >" from <fstream>.
By inserting a reset command like -- using namespace; -- of the used
namespace before the including of <strstream.h>,
one can avoid this kind of problem.
I know that one cannot intermix <iostream> and <iostream.h> but when an
application is
using one library with <iostream.h> and another one with <iostream> comming
from 2 differents
providers, this is a real painful problem!!!
In my opinion, the -- using namespace;-- could be a easy solution both for
compiler and integrator
to cope with the problem.
Cheers
Francis ANDRE
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]