473,396 Members | 1,998 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,396 software developers and data experts.

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 2878

"trying_to_learn" <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::string);
};
#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::string const &);

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

--
Bob Hairgrove
No**********@Home.com
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: 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...
6
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,...
10
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
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...
13
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
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" <<...
6
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...
11
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...
0
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,...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.