![]() |
|
|
Welcome to the { mindfrost82.com } forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
How to use namespace in a class definition?
I try to use the namespace in a class definition as follows,
#include <iostream> class raw_data{ using namespace std; // Here, the compiler gave an error mesage "syntax error" ..... }; If the 'using namespace std' is put before the class raw_data, it's OK. But I don't know why the above code segment cannot work. Is it forbiden for C++ to use embeded namespace? Could somebody here give me some hint on that? Thanks in advance. Z.L. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: How to use namespace in a class definition?
Z.L. wrote:
> class raw_data{ > using namespace std; // Here, the compiler gave an error mesage > "syntax error" > .... > }; > > If the 'using namespace std' is put before the class raw_data, it's > OK. But I don't know why the above code segment cannot work. Is it > forbiden for C++ to use embeded namespace? Yes, this is simply not supported. However, what you can do is being a bit more specific with what you want: using std::istream; Further, your terminology is a bit misleading, because embedding could be interpreted as nesting or importing. A using directive imports the named symbols into the current namespace. Further, there are nested namespaces and classes, i.e. namespaces or classes within other namespaces and classes. You can nest both of them inside each other and you can nest classes inside namespaces, but you can not nest namespaces inside classes. If you need that, you can use a nested class (or structure) with only public and static members as workaround. Uli -- Sator Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932 [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: How to use namespace in a class definition?
On 2008-08-19 11:58, Z.L. wrote:
> I try to use the namespace in a class definition as follows, > > #include <iostream> > > class raw_data{ > using namespace std; // Here, the compiler gave an error mesage > "syntax error" > .... > }; > > If the 'using namespace std' is put before the class raw_data, it's > OK. But I don't know why the above code segment cannot work. Is it > forbiden for C++ to use embeded namespace? > > Could somebody here give me some hint on that? The C++ standard is quite clear on this (it is the first sentence in the section): "A using-directive shall not appear in class scope, but may appear in namespace scope or in block scope." -- Erik Wikström [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
|
|||
|
Re: How to use namespace in a class definition?
<snip everything>
it is btw pity that this isn't possible. By specifing those 'using' statements on global level, especially in header files, it will be picked up by every client of the header file. So thats imo not an option. So then you must specify the complete namespace which is a lot of typing and makes it quite unreadable (real life example of the handy Boost.MultiIndex, often used in classes as replacement of STL containers): class AcclReceivers { //... typedef std::pair<CWnd*, int> Entry; typedef boost::multi_index::multi_index_container < Entry, boost::multi_index::indexed_by < boost::multi_index::ordered_unique< boost::multi_index::identity<Entry> >, boost::multi_index::ordered_non_unique< boost::multi_index::member<Entry, CWnd*, &Entry::first> >, boost::multi_index::ordered_non_unique< boost::multi_index::member<Entry, int, &Entry::second> > > > EntrySet; EntrySet m_Entries; }; Of course it can be solved by a global namespace alias, or putting the class in its own namespace (and then one has the 'using' options back), but this alias is only needed within the class for readability improvements, so ideally it would only be put there. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
![]() |
|
| Thread Tools | Search this Thread |
| Display Modes | |
|
|