using namespace std
has anyone used this before? my friend uses it, but i never used it in all of my computer engineering classes, ever. is it a microsoft thing in vc++ 6? and what does it do. and also, for #includes, do people include the h, or not?
iostream vs iostream.h
and what''s the difference
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
April 20, 2000 01:04 AM
The 1996 C++ working draft specifies that there are standard header files such as or . This may be confusing to some of you who have always used . However, that header file does not exist in the C++ standard and is only included for backwards compatibility.
But... Including the header file is not enough. The standard C++ library definitions are included in namespace std. If you don''t know what a namespace is, just remember that after you include or or or you must write this line of code:
using namespace std;
It brings the entire ''std'' namespace into global scope so that you may access it all normally.
string s = "blah";
cout << s << endl;
I hope that clarifies everything. ^_^
If at all possible, tell your teachers in Cpr E to use modern C++. The faster we can get all this straightened out the better everything will be.
aegisknight
aegisk@iastate.edu
But... Including the header file is not enough. The standard C++ library definitions are included in namespace std. If you don''t know what a namespace is, just remember that after you include or or or you must write this line of code:
using namespace std;
It brings the entire ''std'' namespace into global scope so that you may access it all normally.
string s = "blah";
cout << s << endl;
I hope that clarifies everything. ^_^
If at all possible, tell your teachers in Cpr E to use modern C++. The faster we can get all this straightened out the better everything will be.
aegisknight
aegisk@iastate.edu
April 20, 2000 01:09 AM
Oops, sorry about that. I didn''t realize this thing used HTML.
Anyway, those blank parts in the sentences are supposed to be header files, such as:
<iostream>
<fstream>
<strstream>
<vector>
<stack>
<string>
aegisknight
Anyway, those blank parts in the sentences are supposed to be header files, such as:
<iostream>
<fstream>
<strstream>
<vector>
<stack>
<string>
aegisknight
Some additional info:
Instead of specifying ''using namespace std'' (which makes every ''item'' in the std namespace available), you can also specify std:: (scope specifier) before things from the std namespace. For example, you could use
std::cout << "Hello world!" << std::endl;
Another option is to use ''using std::'' followed by the identifier. For example:
using std::cout;
using std::endl;
cout << "Hello world!" << endl;
The primary reasons for not ''opening'' up the complete namespace is prevention of namespace pollution. When a lot of identifiers can be used without explicit namespace scope specifiers, chances are that you have some name collision.
You can also rename namespaces, for example if their names are a bit to long to use. Example:
namespace very_long_name_for_a_namespace
{
...
}
namespace simple = very_long_name_for_a_namespace;
Erik
Instead of specifying ''using namespace std'' (which makes every ''item'' in the std namespace available), you can also specify std:: (scope specifier) before things from the std namespace. For example, you could use
std::cout << "Hello world!" << std::endl;
Another option is to use ''using std::'' followed by the identifier. For example:
using std::cout;
using std::endl;
cout << "Hello world!" << endl;
The primary reasons for not ''opening'' up the complete namespace is prevention of namespace pollution. When a lot of identifiers can be used without explicit namespace scope specifiers, chances are that you have some name collision.
You can also rename namespaces, for example if their names are a bit to long to use. Example:
namespace very_long_name_for_a_namespace
{
...
}
namespace simple = very_long_name_for_a_namespace;
Erik
woah. i didn''t know namespaces were so important. it was never taught to us CmpE''s, and my friend who''s a CS major uses it all the time. is there anywhere i can read up more on this, cuz this may be the reason for some linker errors i get.
a2k
a2k
------------------General Equation, this is Private Function reporting for duty, sir!a2k
Every recent C++ book that discusses Standard C++ should have info on namespaces. ''C++ primer, 3rd edition'' has info on them for sure.
Namespace problems are more likely to cause compiler errors than linker errors, btw. One thing to take notice of is that mixing (for example) <iostream> and <iostream.h> includes in different files in VC++6 is very likely to cause problems. I experienced them myself
Erik
Namespace problems are more likely to cause compiler errors than linker errors, btw. One thing to take notice of is that mixing (for example) <iostream> and <iostream.h> includes in different files in VC++6 is very likely to cause problems. I experienced them myself
Erik
Yes,
As for whether or not to include the .h in include files: If at all possible (i.e. your C++ compiler and other tools are not getting stale) do not put the .h on the end. Before everything was standardized, .h was the convention, and all the compilers used that with the headers putting everything in global namespace. Once they added namespaces to the standard, and realized just how polluted the standard libaries would make everything, they decided to put all of the standard libraries in std::.
Of course, this presented a small problem, because what do you do about backward compatability and such... so they decided to standardize on using headers without the .h, and letting compilers use the .h versions to make users'' lives easier during the transition...
-Brian
As for whether or not to include the .h in include files: If at all possible (i.e. your C++ compiler and other tools are not getting stale) do not put the .h on the end. Before everything was standardized, .h was the convention, and all the compilers used that with the headers putting everything in global namespace. Once they added namespaces to the standard, and realized just how polluted the standard libaries would make everything, they decided to put all of the standard libraries in std::.
Of course, this presented a small problem, because what do you do about backward compatability and such... so they decided to standardize on using headers without the .h, and letting compilers use the .h versions to make users'' lives easier during the transition...
-Brian
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement