Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > "using namespace" within a class declaration?

Reply
Thread Tools

"using namespace" within a class declaration?

 
 
Jacek Dziedzic
Guest
Posts: n/a
 
      04-28-2004
Is it valid to use a "using namespace foo" (as opposed to
using foo::bar which I'm sure is legal) within a class
declaration? My compiler rejects it, but I've been told it's
valid.

Can anyone please confirm or deny?

TIA,
- J.
 
Reply With Quote
 
 
 
 
E. Robert Tisdale
Guest
Posts: n/a
 
      04-28-2004
Jacek Dziedzic wrote:

> Is it valid to use a "using namespace foo"
> (as opposed to using foo::bar which I'm sure is legal)
> within a class declaration?
> My compiler rejects it, but I've been told it's valid.
>
> Can anyone please confirm or deny?



> cat test.cc

#include <iostream>

class X {
private:
// representation
int I;
public:
using namespace std;
using std::endl;
X(int i = 0): I(i) {
cout << "X(" << i << ")" << endl;
}
};

int main(int argc, char* argv[]) {
X x(13);
std::cout << "The end!" << std::endl;
return 0;
}

> g++ -Wall -ansi -pedantic -o test test.cc

test.cc:8: parse error before `namespace'
test.cc:9: using-declaration for non-member at class scope
test.cc: In constructor `X::X(int)':
test.cc:11: `cout' undeclared (first use this function)
test.cc:11: (Each undeclared identifier is reported \
only once for each function it appears in.)
test.cc:11: `endl' undeclared (first use this function)
test.cc: In function `int main(int, char**)':
test.cc:16: warning: unused variable `X x'

 
Reply With Quote
 
 
 
 
John Carson
Guest
Posts: n/a
 
      04-29-2004
"Jacek Dziedzic" <jacek__NOSPAM__@janowo.net> wrote in message
news:c6p955$hje$
> Is it valid to use a "using namespace foo" (as opposed to
> using foo::bar which I'm sure is legal) within a class
> declaration? My compiler rejects it, but I've been told it's
> valid.
>
> Can anyone please confirm or deny?
>
> TIA,
> - J.


Both Comeau online and VC++ 7.1 reject both and say that using declarations
must involve base class names. For example, the following won't compile:

#include <iostream>
class A
{
public:
using std::cout;
A()
{
cout << "A constructed\n";
}
};

nor will

#include <iostream>
class A
{
public:
using namespace std;
A()
{
cout << "A constructed\n";
}
};

On the other hand, if you move the using directive/declaration inside a
function, it is a different story. The following is fine:

class A
{
public:
A()
{
using namespace std;
using std::cout;
cout << "A constructed\n";
}
};


--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why? Partial Class within a Partial class Billy ASP .Net 2 02-01-2006 09:10 AM
Class A contains class B, class B points to class A Joseph Turian C++ 5 12-30-2005 03:24 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
A parameterized class (i.e. template class / class template) is not a class? christopher diggins C++ 16 05-04-2005 12:26 AM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



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