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.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-19-2008, 10:58 AM
Z.L.
 
Posts: n/a
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! ]

Reply With Quote
  #2 (permalink)  
Old 08-19-2008, 10:22 PM
Ulrich Eckhardt
 
Posts: n/a
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! ]

Reply With Quote
  #3 (permalink)  
Old 08-19-2008, 10:26 PM
=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
 
Posts: n/a
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! ]

Reply With Quote
  #4 (permalink)  
Old 08-20-2008, 10:10 PM
gast128@hotmail.com
 
Posts: n/a
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! ]

Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > C++


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 12:17 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Loans | Mobile Phone | Yugioh | Montana Music | Free Advertising



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114