Re: Iostream

From: Mike Wahler (mkwahler_at_mkwahler.net)
Date: 10/24/03


Date: Thu, 23 Oct 2003 22:42:44 GMT


"ncstate" <member45487@dbforums.com> wrote in message
news:3517496.1066944307@dbforums.com...
>
> it seems when i try to compile this simple code I get an error message i
> believe is from g++ not finding the IOSTREAM header file.

No, it's telling you it cannot find the declaration of
an identifier you've used ('cout').

>i found the
> header file in /usr/include/c++/3.2.2/ so that is why it is included
> like that. suggestions?

Don't include it 'like that'.

> I'm running red hat 9

This doesn't matter. C++ is a platform independent language.

> code :
>
> #include </usr/include/c++/3.2.2/iostream>

Change to:

#include <iostream>

If this does not work, then your compiler is installed
and/or configured incorrectly.

Note that from the language's perspective <iostream> is a
standard *header* name, it does *not* signify a "file" name.
Many if not most implementations do provide an actual file
with the same or a similar name to implement a standard header,
but this is an implementation detail, not specified or
required by the language. E.g. a compiler could provide the
required declarations as 'hard coded' if it wanted and would
still be conforming -- the #include statement would still be
required however).

[Gratuitous whitespace removed from code below]

> int main()
> {
> cout << endl << endl << "Jeff" << endl;
> cout << "E115 Sec 010 B" << endl;
> cout << "Hello World" << endl << endl << endl;
>
> return 0;
> }
>
> errors:
>
> project2.C: In function `int main()':
>
> project2.C:6: `cout' undeclared (first use this function)

This is because 'cout' (and all standard library identifiers
except macros are declared in namespace 'std').

> project2.C:6: (Each undeclared identifier is reported only once for each
>
> function it appears

#include <iostream>

int main()
{
    std::cout << "Hello world\n";
    return 0;
}

or

#include <iostream>

using std::cout;

int main()
{
    cout << "Hello world\n";
    return 0;
}

or

#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world\n";
    return 0;
}

(If desired, the scope of 'cout' can be further restricted
 by putting the 'using' directive or declaration inside the
 'main()' function.)

Note: while most implementations will work without it,
the declaration for 'endl' is provided by <ostream>.
It is allowed to be, but not required to be, provided by
 <iostream>. 'endl' is also in namespace 'std'. (Its
'full name' is 'std::endl')

BTW, which C++ book(s) are you reading?

-Mike



Relevant Pages

  • Re: Confusion in ANSI Cs function concepts
    ... is no function declaration corresponding to the function call and the ... should assume a declaration with an int return type. ... the compiler will assume that fun is a function returning int ... in the above case the return type is mentioned as void *. ...
    (comp.lang.c)
  • Re: operator function
    ... >> of them have anything to do with what the error message seems ... Setting (int h, int m, int s, int TT) ... Now the compiler knows, that it is safe to use that function on a const ... >> operator returns a Setting object. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Inconsistent Program Results
    ... If you don't list the parameter types in the declaration, ... the compiler won't be able to track these places down quickly for you. ... mainreturns int. ... I've written a textbook in which I state that main ...
    (comp.lang.c)
  • Re: How do I create a function in my library for passing user callback function
    ... int my_callback_return; ... matches the declaration in the header file: ... in your source file, as well as any other functions that you have ... I'm surprised the compiler didn't get more upset. ...
    (comp.lang.c)
  • Re: void * vs char *
    ... cast p to a (struct s*) to avoid a compile-time error. ... struct s {int a;}; ... function call consists solely of an identifier, and if no declaration ... And see what your compiler has to say. ...
    (comp.lang.c)