472,805 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

"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.
Jul 22 '05 #1
2 6321
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'

Jul 22 '05 #2
"Jacek Dziedzic" <ja*************@janowo.net> wrote in message
news:c6**********@korweta.task.gda.pl
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)

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: beliavsky | last post by:
Many of my C++ programs have the line using namespace std; but the "Accelerated C++" book of Koenig and Moo has many examples where the library names are included one at a time, for example ...
8
by: Douglas | last post by:
**** Post for FREE via your newsreader at post.usenet.com **** Hello, The following code does not compile if line 3 is uncommented "using namespace std". I do not understand it. Could...
14
by: john.burton.email | last post by:
I've done some extensive searching and can't seem to find an answer to this - Is it correct to using "using" with templates, for example: using std::vector; Or do I need to specify the type...
5
by: SenthilSS | last post by:
My application produces XML Data files which have XML namespace qualified XML elements (nodes), but the namespace itself is not declared in the data file. My task is to read these data files in a...
6
by: AlexD_UK | last post by:
When I create a new C++ project of type "Class Library (.NET)", I am unable to then add the following line of code : using namespace std If I do, I get the following error on compilation :...
30
by: Pep | last post by:
Is it best to include the code "using namespace std;" in the source or should each keyword in the std namespace be qualified by the namespace tag, such as std::cout << "using std namespace" <<...
12
by: Steve Pope | last post by:
Compiling the following works on my system: file main.cpp: #include <iostream> namespace space { int foo; }
0
by: Andreas Schmitt | last post by:
I wrote a small timer class for use in a graphics engine I am working on for teaching myself. The small time based animations I tried with it seem to work fine but displaying the frame rate I...
25
by: samjnaa | last post by:
Please check for sanity and approve for posting at python-dev. In Visual Basic there is the keyword "with" which allows an object- name to be declared as governing the following statements. For...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.