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 2 5548
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
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>. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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;...
|
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...
|
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...
|
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....
|
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...
|
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");
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
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...
|
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...
|
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
...
|
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...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
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=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |