473,749 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using namespace directive in header file

im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_ H
#define FIRSTOOPSTRUCT_ H

struct firstoopstruct
{
int data;
void behav(string);
};
#endif
Jul 22 '05 #1
2 2914

"trying_to_lear n" <no****@no.no > wrote in message
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_ H
#define FIRSTOOPSTRUCT_ H #include<string >
struct firstoopstruct
{
int data;
void behav(string);
void behav(std::stri ng);
};
#endif


Address string as std::string.

Sharad
Jul 22 '05 #2
On Sun, 31 Oct 2004 23:34:17 -0800, trying_to_learn <no****@no.no >
wrote:
im in the primary stages of learning C++. The book im learning from says
Dont use using namespace.. directive in header file
However im trying to make the following header file.I need to include
<string> header file and also will have to say using namespace std to
make the string class visible.
You do not have to do it this way. Unfortunately, most example
programs do this, but it is a bad habit.
qn)how do i resolve the requirement of *not* using the "using" directive
in the header file and at the same time declare my structure in the
header file?.Pls excuse me if this is a dumb qn. im just starting out

#ifndef FIRSTOOPSTRUCT_ H
#define FIRSTOOPSTRUCT_ H

struct firstoopstruct
{
int data;
void behav(string);
};
#endif


You must either write std::string, instead of just string, or you can
use a typedef:

typedef std::string string;

I always write out std::string (or use a different typedef alias)
because it documents to other programmers, who may have to maintain
your code in the future, the fact that you did not have "using
namespace std;" in this or some other header file. IOW, if I were the
"other programmer", and saw "string" written throughout your code but
overlooked the typedef, I might have come to the erroneous conclusion
that there is a "using namespace std;" somewhere else and waste some
time looking for it.

Note that it is legal C++ to write "using namespace std;" in a header
file, but the results of doing so can be catastrophic ... please take
the trouble to understand WHY it is strongly recommended not to do so
(google for "namespace pollution", for example). It is perfectly OK to
put it in a .cpp file, but only AFTER including any other headers.
Better yet, use it only inside each function where it is needed, or
put "using std::string;"-type declarations inside the functions
(google for the difference between "using directive" and "using
declaration" ... I always mix them up, anyway <g>).

BTW you might consider declaring the function "behav" like this:

void behav(std::stri ng const &);

This will usually perform better because it eliminates the extra
temporary string copy you would otherwise have.

--
Bob Hairgrove
No**********@Ho me.com
Jul 22 '05 #3

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

Similar topics

17
3514
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 using std::cin; using std::cout;
8
4915
by: Petter Reinholdtsen | last post by:
I ran into a problem on HP-UX 11.00 the other day, where it refused to compile a program using 'using namespace std;' at the top. The reason seem to be that the compiler refuses to accept 'using namespace std;' unless the std namespace was declared first. This triggered my curiosity, and I tried to find out what the ANSI C++ standard had to say about this. I'm unable to find a conclusion, and hope someone here have a clue to spare. ...
6
2939
by: Gernot Frisch | last post by:
hi, for terrible reasons my stdafx.h has: using namepsace std; in it. Now, how can I "un-use the namespace in a cpp file, that _must_ include this header? -- -Gernot int main(int argc, char** argv) {printf
10
1826
by: Christian Christmann | last post by:
Hi, are there any drawbacks using "using namespace std" instead of defining the required namespaces like using std::cout
12
3380
by: Calum Grant | last post by:
In older C++ computer books, you'll often see using namespace std; even in my 1996 copy of Stroustrup. Nowadays, it seems to be considered better to qualify names to make it clearer what symbol you are using. Are there any articles that support this viewpoint? Is "using namespace" definitively wrong, or just a matter of style? How about when implementing a library "foo", would it not make the code
13
141253
by: Squid Seven | last post by:
This is just bizarre. for the following snippet of code: #include <string> using std::string; I get the error message:
30
4117
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" << std::endl; Myself I am not sure which I prefer, it is certainly easier to specify that the std namespace is being used instead of tagging each member of the namespace?
6
9014
by: Halcyon | last post by:
so i've been "using namespace std" happily in all my source files at the global scope, and i then go to to use cout, vector, string etc without having to use std:: everytime. However today i was informed that i should eschew "using namespace std" and use "using std::string", "std::vector" et al. I don't quite get what the potential drawbacks of "using namespace std" are... i perused a few books, but most authors *do* use "using...
11
2541
by: subramanian100in | last post by:
Suppose the following is in Test.h #ifndef TEST_H #define TEST_H #include <iostream> #include <string> using namespace std;
0
8832
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9388
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.