473,388 Members | 1,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 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 6365
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.