 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Michael Auton Guest
|
Posted: Fri Jun 27, 2003 4:49 pm Post subject: can some one explain.....standard library (std) & <iostream> |
|
|
I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
#include <iostream>
int main()
{
std::cout << "Hello world!n";
...
}
Ive read that cout is in the std library, so thats why you need
std::cout.
But ive also read that cout is part of
<iostream>.
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
Or am I looking at this from the wrong angle? Anyone please explain
this, as I can see that im going to need to understand this for
further functions.
many thanks
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
MiniDisc_2k2 Guest
|
Posted: Sat Jun 28, 2003 12:32 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
You're really confusing yourself here...
"Michael Auton" <antonhs (AT) blueyonder (DOT) co.uk> wrote
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
|
That's just how the standard was written.
| Quote: |
#include <iostream
int main()
{
std::cout << "Hello world!n";
...
}
Ive read that cout is in the std library, so thats why you need
std::cout.
|
Yes it is.
| Quote: | But ive also read that cout is part of
iostream>.
|
Yes it is.
You're in school aren't you. I wish they would stop teaching the header
files to be called "libraries." (at least my teacher did) It can make things
get really confusing.
| Quote: |
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
|
Yes but there's something called "scope." Take this example:
class A
{
class B
{
public: int a;
};
};
B b; // this is illegal
That is illegal because it is "out of scope." The braces define the scope,
if you will. If you are outside of that set of braces you are not allowed to
access anything within the braces. This applies the same to variables
declared within do-while loops, and anything else which uses braces. In
fact, braces are so important that they actually can let you get away with
things. You're not allowed to initialize variables within a switch/case
statement, but with braces you can "close the scope" and initialize them in
there all you want. Anyways what I meant to get at is for the same reason
you're not allowed to access B from "global scope," you're not allowed to
access cout from "global scope." You must access it _through_ std.
| Quote: |
Or am I looking at this from the wrong angle? Anyone please explain
this, as I can see that im going to need to understand this for
further functions.
|
Yes you will definately need to understand this for later. It's the basic
concept behind object oriented programming and minimal visibility. You'll
probably want to do a google search or something to get information on
scope, as I could just scrape the surface here.
No problem
-- MiniDisc_2k2
P.S. If you really must know (I'm not quite sure if this is standard,
though), you could #include <iostream.h> which enables you to just use cout.
It's a little different from <iostream>. Also, read up on the "using"
declaration, as that will also allow you to circumvent std (although I don't
know how, I just use <iostream.h>).
To reply, replace nospam.com with cox dot net.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Jun 28, 2003 12:33 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
"Michael Auton" <antonhs (AT) blueyonder (DOT) co.uk> wrote
| Quote: | Ive read that cout is in the std library, so thats why you need
std::cout.
But ive also read that cout is part of <iostream> so thats why I need
iostream>.
|
More properly, cout is defined in the iostream header. This is why you need it.
It is declared in that header to be in the std namespace. Look up namespaces
in your C++ book. You have to either qualify it with std:: or use using to bring
it into the scope of where you use it.
| Quote: |
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
|
iostream is NOT a library. This isn't Java. iostream provides the necessary
definitions so that you can use the standard library functions.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Drew Hall Guest
|
Posted: Sat Jun 28, 2003 12:47 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
"Michael Auton" <antonhs (AT) blueyonder (DOT) co.uk> wrote
| Quote: | Ive read that cout is in the std library, so thats why you need
std::cout.
But ive also read that cout is part of <iostream> so thats why I need
iostream>.
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
|
The "standard library" is the set of classes & functions defined in the
language standard, all of which are made accessible by #including
various headers. <iostream> is one of these headers, and is thus part
of the standard library (along with <string>, <vector>, etc). The parts
of the standard library that are not directly inherited from C all live in
a namespace that is specially reserved for them, "std". This includes
all of the headers that don't end in ".h". This prevents the names of the
standard library components from interfering with names in code that
you develop.
So, you need to say #include <iostream> to make the std. library
iostream components accessible, then use "std::" to make it clear that
when you say "cout", you mean the cout object that lives in the std
namespace (and whose declaration lives in <iostream>.
Sounds like you need a good tutorial or book to help you get
comfortable with the lay of the land. I recommend Koenig & Moo's
"Accelerated C++" or Lippman & Lajoie's "C++ Primer".
Good luck!
Drew
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Artie Gold Guest
|
Posted: Sat Jun 28, 2003 12:54 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
Michael Auton wrote:
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
|
The header <iostream> declares the stream "cout", which is in the
_namespace_ "std".
| Quote: |
#include <iostream
int main()
{
std::cout << "Hello world!n";
...
}
Ive read that cout is in the std library, so thats why you need
std::cout.
But ive also read that cout is part of
iostream>.
|
Yes, the header <iostream> declares the stream "cout", which is in the
_namespace_ "std".
| Quote: |
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
|
Yes, the header <iostream> declares the stream "cout", which is in the
_namespace_ "std".
| Quote: |
Or am I looking at this from the wrong angle? Anyone please explain
this, as I can see that im going to need to understand this for
further functions.
|
You were. You will. ;-)
HTH,
--ag
--
Artie Gold -- Austin, Texas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sebastian Moleski Guest
|
Posted: Sat Jun 28, 2003 12:55 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
"Michael Auton" <antonhs (AT) blueyonder (DOT) co.uk> wrote
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
|
The standard library is distributed over a number of header files. When you
include <iostream>, you really only get access to the I/O streaming part of
the standard library. Now, despite the fact that the standard library is
split up like that over different files, all of its contents reside in one
common namespace: std. The standard committee could have decided to host
every part of the library in its own namespace (and thus you would have
iostreams::, vector::, list::, etc.), but they decided that one namespace
should be sufficient.
HTH,
sm
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
gs_sarge@earthlink.net Guest
|
Posted: Sat Jun 28, 2003 11:05 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
I had this explained just the other day in class
when using the <iostream> without the .h extension, you are using the
new naming method (forgive me if my terminology is off) where all
standard library functions must use the std namespace. However, to
keep backwards compatible, if you include iostream.h, with the .h
appended to the end, it will keep the global variable cout, which
allows you to access without the namespace std.
Hope that helps,
On 27 Jun 2003 12:49:35 -0400, [email]antonhs (AT) blueyonder (DOT) co.uk[/email] (Michael
Auton) wrote:
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
#include <iostream
int main()
{
std::cout << "Hello world!n";
...
}
Ive read that cout is in the std library, so thats why you need
std::cout.
But ive also read that cout is part of
iostream>.
Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
Or am I looking at this from the wrong angle? Anyone please explain
this, as I can see that im going to need to understand this for
further functions.
many thanks
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
llewelly Guest
|
Posted: Sat Jun 28, 2003 11:32 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
"MiniDisc_2k2" <MattDelB (AT) nospam (DOT) com> writes:
[snip]
| Quote: | P.S. If you really must know (I'm not quite sure if this is standard,
though),
|
This is not standard.
| Quote: | you could #include <iostream.h> which enables you to just use cout.
|
In the long run, I think this is a bad idea. While implementations of
<iostream> are converging towards the standard (and therefor
becoming more and more similar with each compiler release),
<iostream.h> implementations are either stagnant or
diverging. Which is currently more portable is an open question,
but soon using <iostream> will be more portable. Worse,
<iostream.h> has no future; it is not part of the standard, and
most implementors do not wish to maintain it indefinitely.
| Quote: | It's a little different from <iostream>. Also, read up on the "using"
declaration, as that will also allow you to circumvent std (although I don't
know how, I just use <iostream.h>).
[snip] |
I advise you to change now before the transition period is
over. (Since there is no official transition period, there's no
telling when that will be.)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Kurt Krueckeberg Guest
|
Posted: Sat Jun 28, 2003 11:34 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
#include
int main()
{
std::cout << "Hello world!n";
...
}
|
The standard library is defined in a namespace called "std". That's why
"std::cout" is needed.
To get around having to write "std::cout", add a using declaration for
cout
using std::cout;
or insert the using directive
using namespace std;
The later will make all std names part of the global namespace.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Corey Murtagh Guest
|
Posted: Sun Jun 29, 2003 11:34 am Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
Kurt Krueckeberg wrote:
<snip>
| Quote: |
The standard library is defined in a namespace called "std". That's why
"std::cout" is needed.
To get around having to write "std::cout", add a using declaration for
cout
using std::cout;
or insert the using directive
using namespace std;
The later will make all std names part of the global namespace.
|
This is generally considered to be a bad idea. There are a lot of
things in the 'std' namespace, and one trusts that there is a good
reason *why* they are there rather than in the global namespace.
I've gotten comfortable over the past few months (since I finally
started to actually use the STL, after years of using C++) with typing
namespace specifiers before the STL objects (not to mention boost, etc).
If it becomes too cumbersome I will occasionaly throw in a 'using'
directive at function scope. I always consider the effects of doing so
very carefully however.
--
Corey Murtagh
The Electric Monk
"Quidquid latine dictum sit, altum viditur!"
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Jun 29, 2003 12:34 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
In message <3efcd4e8.266470954 (AT) news (DOT) earthlink.net>,
[email]gs_sarge (AT) earthlink (DOT) net[/email] writes
| Quote: | I had this explained just the other day in class
when using the <iostream> without the .h extension, you are using the
new naming method (forgive me if my terminology is off) where all
standard library functions must use the std namespace. However, to
keep backwards compatible, if you include iostream.h, with the .h
appended to the end, it will keep the global variable cout, which
allows you to access without the namespace std.
Hope that helps,
|
In so far as any wrong explanation can help:-)
While it is probably true that a cout provided by iostream.h will in
fact be bound to the same object as std::cout provided by iostream there
is absolutely no requirement that it be so.
There are strict rules about the lifetime of std::cout that are
different from the pre-standard rules of iostream.h.
Imagine mixing an old (pre-namespace) iostream.h with code that uses
std::cout.
It is all very well in student code, but the potential for problems in
real code is just too high and getting higher by the year.
--
ACCU Spring Conference 2003 April 2-5
The Conference you should not have missed
ACCU Spring Conference 2004 Late April
Francis Glassborow ACCU
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Jun 30, 2003 6:34 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
[email]antonhs (AT) blueyonder (DOT) co.uk[/email] (Michael Auton) wrote in message
news:<def74e4c.0306270213.57d0d1fc (AT) posting (DOT) google.com>...
| Quote: | I can understand the simple code below, but I cant get my head around
why I need #include <iostream> and also std:: to call the cout
function.
#include <iostream
int main()
{
std::cout << "Hello world!n";
...
}
Ive read that cout is in the std library, so thats why you need
std::cout.
|
Correctly speaking, the standard defines an entity whose name is
std::cout. This entity is part of the library, which is another way of
saying that its presence isn't automatic -- you have to say that you
want it.
| Quote: | But ive also read that cout is part of
why I need <iostream>.
|
#include <iostream> is the way of activating a certain part of the
library. The standard is a bit wishy-washy here -- when you activate
any part of the library, the implementation may activate other parts as
well. Or it may not. In the case of your program, you actually have to
activate two parts:
#include <iostream> // for std::cout, etc.
#include <ostream> // for the << operators
Some implementations (but not all) also activate the << operators when
you specify "#include
| Quote: | Although when I compile I actually need both! Would I be correct in
saying that iostream contains the std library which contains the cout
function??
|
No. First, technically speaking, <iostream> isn't necessarily a file or
anything like that. All the language guarantees is that when you
include <iostream>, certain parts of the language are activated. In the
case of <iostream>, all that is required to be activated are the
external declarations for the standard input and output objects; as
external declarations, the types are not necessarily complete, and the
operators are not necessarily declared. Including <ostream> activates
the definition of std::istream (which is the type of std::cout), and
most of the << operators.
Practically, of course, the technique that most (all?) implementations
use for this is to put the declarations in files, and read the files.
Exactly as with your header files.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, Tél. : +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Jul 06, 2003 10:44 pm Post subject: Re: can some one explain.....standard library (std) & <iostr |
|
|
Francis Glassborow <francis.glassborow (AT) ntlworld (DOT) com> writes:
| Quote: | In message <3efcd4e8.266470954 (AT) news (DOT) earthlink.net>,
[email]gs_sarge (AT) earthlink (DOT) net[/email] writes
I had this explained just the other day in class
when using the <iostream> without the .h extension, you are using
the new naming method (forgive me if my terminology is off) where
all standard library functions must use the std namespace.
However, to keep backwards compatible, if you include iostream.h,
with the .h appended to the end, it will keep the global variable
cout, which allows you to access without the namespace std.
Hope that helps,
In so far as any wrong explanation can help:-)
While it is probably true that a cout provided by iostream.h will
in fact be bound to the same object as std::cout provided by
iostream there is absolutely no requirement that it be so.
|
Actually, it will only be the case in implementations which don't have
too much respect for their users. The interface of the classical cout
is simply not the same as that of std::cout, and while you won't
notice the difference in most uses, there are one or two which will
bite you; in order to avoid breaking working programs which include
<iostream.h>, the cout which it reveals must have a different type
than std::cout.
Several compiler providers have ignored backwards compatibility in
this regard. For this reason, where I am currently working, we are
stuck having to compile in mode compat=4 with Sun compilers, and we
can't upgrade from g++ 2.95.2. (An additional effect, of course, is
that in future projects, I will warn my customers against using these
compilers.)
| Quote: | There are strict rules about the lifetime of std::cout that are
different from the pre-standard rules of iostream.h.
|
That's one point where the differences are, in practice, almost
inexistant. The only one I can think of is that the classical
iostream.h contained an instance of ios::Init, whereas the standard
doesn't require this of any standard header. In practice, however,
all of the more recent libraries I've used do include it in
<iostream>.
| Quote: | Imagine mixing an old (pre-namespace) iostream.h with code that
uses std::cout.
|
That is, of course, a different problem. Logically, it shouldn't be a
problem -- you have two completely separate systems, both synchronized
with a third (FILE*), but in practice, it doesn't seem to work.
--
James Kanze mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France Tel. +33 1 41 89 80 93
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|