Connecting Tech Pros Worldwide Help | Site Map

"using namespace" within a class declaration?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 10:20 AM
Jacek Dziedzic
Guest
 
Posts: n/a
Default "using namespace" within a class declaration?

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.

  #2  
Old July 22nd, 2005, 10:20 AM
E. Robert Tisdale
Guest
 
Posts: n/a
Default Re: "using namespace" within a class declaration?

Jacek Dziedzic wrote:
[color=blue]
> 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?[/color]

[color=blue]
> cat test.cc[/color]
#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;
}
[color=blue]
> g++ -Wall -ansi -pedantic -o test test.cc[/color]
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'

  #3  
Old July 22nd, 2005, 10:20 AM
John Carson
Guest
 
Posts: n/a
Default Re: "using namespace" within a class declaration?

"Jacek Dziedzic" <jacek__NOSPAM__@janowo.net> wrote in message
news:c6p955$hje$3@korweta.task.gda.pl[color=blue]
> 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.[/color]

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)

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.