473,325 Members | 2,860 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,325 software developers and data experts.

confusion: include and namespace

after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...

and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

....

}

string.h

namespace std
{
class basic_string;
etc

....

}
and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
My question: what is going on, how does namespace and include
communicate? How can I tell compiler to include my .h files into my (or
maybe std) namespace?

reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


THANKS THANKS THANKS

Jul 23 '05 #1
2 5581
puzzlecracker wrote:
after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...

and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

...

}

string.h

namespace std
{
class basic_string;
etc

...

}

The classes you mentioned are all conatined in the std namespace.
Typing "using namespace std;" does not "import" anything, rather it
tells the compiler to look in the namespace "std" for classes and
objects that it doesn't immediately recognize. Alternatively, if you
choose not to use a using statement, you must use fully qualified names
for objects and classes contained within a namespace. This means typing
std::string rather than string when you want to make a string object.
As another alternative, you can use a statement such as "using
std::string;" or "using std::cout;" as opposed to "using namespace std;"

and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
My question: what is going on, how does namespace and include
communicate?
A "using" statement or qualified name tells the compiler where to look
when it sees something it doesn't recognize.

How can I tell compiler to include my .h files into my (or maybe std) namespace?
If you want to create your own namespace, look at the following code:

ken@ken-wn0vf73qmks ~/c
$ cat namespace.cpp
#include <iostream>
using namespace std;

namespace myNamespace {
void printCString(char *str) {
cout << str << endl;
}
}
int main() {
myNamespace::printCString("Hello World!");
return 0;
}

ken@ken-wn0vf73qmks ~/c
$ g++ -o namespace namespace.cpp

ken@ken-wn0vf73qmks ~/c
$ ./namespace
Hello World!

To the best of my knowledge, the std namespace is reserved, so you
shouldn't try to create anything new inside this namespace.

reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


If you don't include the string header, std::string won't work,
std::endl requires that you include iostream header. Prefixing anything
in a namespace in the way you describe is called using a fully qualified
name. It's how you tell the compiler where to look for something.


THANKS THANKS THANKS


Jul 23 '05 #2
puzzlecracker wrote:
after reading some of the post I found out something rather radical to
my previous understanding: that when you do

#include<iostream>
#include<string>
#include<vector>
etc

compiler puts those .h files into the namespace std -.h files that
contain all the declarations for vector, string, etc...
A header cannot be "in a namespace".
and after using namespace std you import everything inside.
My previous understanding was that each of heretofore mentioned .h
files declaration were already contained within std namespace;
something like that:

iostream.h

namespace std
{
class sstream;
etc

...

}

string.h

namespace std
{
class basic_string;
etc

...

}
and so on..

By including those files, you just let a compiler know where those
definitions are located, and "using namespace" just brings those
declarations into the global namespace.
Yes, and that's right. What makes you now believe that it's not?
My question: what is going on, how does namespace and include
communicate?
They don't communicate at all. #include is just kind of a copy/paste
functionality built into your C++'s preprocessor. It does nothing more than
just insert the code of the header in the place where you put the #include
directive. So actually, #include doesn't have anything to do with
namespaces at all.
How can I tell compiler to include my .h files into my (or
maybe std) namespace?
Standard C++ doesn't allow you to put your own classes into namespace std,
but putting them into your own namespace is easy. Just do it something
like:

namespace MyNamespace
{
class Foo
{
//definition of class Foo
void myfunction();
};
}

And for the implementation:

namespace MyNamespace
{
void Foo::myFunction()
{
//some code
}
}

or:

void MyNamespace::Foo::myFunction()
{
//some code
}
reflection: I noticed that by NOT including #include<string>

Prefixing it with std (std::string.. std::endl) works... why, if it is
not put there by compiler (or is it a default)?


Maybe you #included another header that itself #included <string>.
Jul 23 '05 #3

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

Similar topics

5
by: dharmesh Gupta | last post by:
i have a multifile program namely bpl.cpp-contains main() function idr.h ( class definitions) idr.cpp ( the implementation of the functions in the classes described in idr.h) bpl1.h ( contains...
1
by: Don Tucker | last post by:
Hello, I am receiving the following compile-time error: /opt/include/rw/dcomplex.h line 218 error(1420): argument list for class template "complex" is missing typedef complex DComplex when...
2
by: Lynn | last post by:
I am including a new header file in a existing function that has "using namespace std;" defined in it already (from a long time ago). The new header file has the vector data items specified...
8
by: Roman Töngi | last post by:
I don't yet understand how to divide a C++ project into header and implementation files. The following won't compile: // Apple.cpp #include <iostream> #include "random.h" using namespace std;...
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
9
by: Buz Waitz | last post by:
I'm a newbie. I've created a datalayer class which I creatively called sqlAccess and placed in a namespace I just as creatively called MyDomain.DataAccessLayer. (Well now, I didn't actually use...
1
by: Epetruk | last post by:
Hello, I have a solution with two projects. One of the projects is called MyProj with a root namespace called MyProj.Obj. The single source (vb) file for MyProj has a class called Obj....
5
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.