Iostream

Discussion in 'C++' started by ncstate, Oct 23, 2003.

  1. ncstate

    ncstate Guest

    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. i found the
    header file in /usr/include/c++/3.2.2/ so that is why it is included
    like that. suggestions?

    I'm running red hat 9



    code :



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



    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)

    project2.C:6: (Each undeclared identifier is reported only once for each

    function it appears


    --
    Posted via http://dbforums.com
     
    ncstate, Oct 23, 2003
    #1
    1. Advertising

  2. > 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. i
    > found the header file in /usr/include/c++/3.2.2/ so that is why it is
    > included like that. suggestions?
    >
    > I'm running red hat 9
    >
    > #include </usr/include/c++/3.2.2/iostream>


    I don't know about the requirements of the standard about that, but imho, if
    your compiler forces you to specify such a path, it is broken. Ask in a
    newsgroup supporting it

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9

    > int main()
    > {
    >
    > cout << endl << endl << "Jeff" << endl;


    <snip>

    > errors:
    > project2.C: In function `int main()':
    >
    > project2.C:6: `cout' undeclared (first use this function)


    'cout' is defined in the std namespace (as all standard functions, classes
    and objects are), so you must explictly refer to it :

    std::cout << std::endl << "Jeff" << std::endl;

    Or look up using directive and declaration.


    Jonathan
     
    Jonathan Mcdougall, Oct 23, 2003
    #2
    1. Advertising

  3. "ncstate" <> wrote in message
    news:...
    >
    > 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. i found the
    > header file in /usr/include/c++/3.2.2/ so that is why it is included
    > like that. suggestions?
    >
    > I'm running red hat 9
    >
    >
    >
    > code :
    >
    >
    >
    > #include </usr/include/c++/3.2.2/iostream>
    >
    >
    >
    > 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)
    >
    > project2.C:6: (Each undeclared identifier is reported only once for each
    >
    > function it appears


    The 'cout' object is declared in the 'std' namespace. You should
    either prefix it with "std::" or declare that you're using that
    name:

    using std::cout;
    using std::endl; // as well

    Get a decent book on C++.

    Victor
     
    Victor Bazarov, Oct 23, 2003
    #3
  4. ncstate

    MPBroida Guest

    ncstate wrote:
    >
    > 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. i found the
    > header file in /usr/include/c++/3.2.2/ so that is why it is included
    > like that. suggestions?
    >
    > I'm running red hat 9
    >
    > code :
    >
    > #include </usr/include/c++/3.2.2/iostream>
    >


    It's been a little while, but I seem to recall that
    including a file from the "builtin" include dirs is
    done via:
    #include <filename>
    That tells the compiler to use its own include path(s).

    But including a USER file is done via:
    #include "filename"
    That tells the compiler to use the include paths from
    the compile commandline (or defaults)(or project/etc).

    Try changing the < and > to " and " and see what
    happens.

    Mike
     
    MPBroida, Oct 23, 2003
    #4
  5. ncstate

    David Rubin Guest

    ncstate wrote:
    [snip - rearranged]
    > #include </usr/include/c++/3.2.2/iostream>


    You can just #include <iostream>

    > int main()
    > {
    > cout << endl << endl << "Jeff" << endl;

    [snip]
    > project2.C: In function `int main()':
    > project2.C:6: `cout' undeclared (first use this function)
    > project2.C:6: (Each undeclared identifier is reported only once for each
    > function it appears


    Try std::cout and std::endl or 'using namespace std;' I found this
    diagnostic a bit confusing too the first time I saw it.

    /david

    --
    "As a scientist, Throckmorton knew that if he were ever to break wind in
    the echo chamber, he would never hear the end of it."
     
    David Rubin, Oct 23, 2003
    #5
  6. ncstate

    Mike Wahler Guest

    "ncstate" <> wrote in message
    news:...
    >
    > 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
     
    Mike Wahler, Oct 23, 2003
    #6
  7. ncstate

    Mike Wahler Guest

    "David Rubin" <> wrote in message
    news:COYlb.9479$...
    > > project2.C: In function `int main()':
    > > project2.C:6: `cout' undeclared (first use this function)
    > > project2.C:6: (Each undeclared identifier is reported only once for each
    > > function it appears

    >
    > Try std::cout and std::endl or 'using namespace std;'


    or:
    using std::cout;
    using std::endl;

    >I found this
    > diagnostic a bit confusing too the first time I saw it.


    "'cout' undeclared." Exactly the case. How much more specific
    could it be? :)

    Before you say e.g. "well the compiler should 'know' it's a
    'namespace std' issue, I'll say that no, it cannot (and should not)
    make such an assumption. I might want an identifer of my own named
    e.g. 'cout', which I have every right to define, in either a 'global',
    'file', 'namespace'*, or 'local' scope, and expect no problems until I
    explicitly cause a conflict with a 'using' statement which brings
    'std::cout' into scope.

    (*) except namespace 'std', where the 'user' is not
    allowed to define anything.


    OP's problem is not an 'ambiguous' error message, but insufficient
    background knowledge before using the compiler.

    -Mike
     
    Mike Wahler, Oct 23, 2003
    #7
  8. ncstate

    Mike Wahler Guest

    "MPBroida" <michael.p.broida@boeing_oops.com> wrote in message
    news:3F9858AB.2DF20187@boeing_oops.com...
    > ncstate wrote:
    > >
    > > 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. i found the
    > > header file in /usr/include/c++/3.2.2/ so that is why it is included
    > > like that. suggestions?
    > >
    > > I'm running red hat 9
    > >
    > > code :
    > >
    > > #include </usr/include/c++/3.2.2/iostream>
    > >

    >
    > It's been a little while, but I seem to recall that
    > including a file from the "builtin" include dirs is
    > done via:
    > #include <filename>
    > That tells the compiler to use its own include path(s).
    >
    > But including a USER file is done via:
    > #include "filename"
    > That tells the compiler to use the include paths from
    > the compile commandline (or defaults)(or project/etc).
    >
    > Try changing the < and > to " and " and see what
    > happens.


    Please check your facts before making such suggestions.

    -Mike
     
    Mike Wahler, Oct 23, 2003
    #8
  9. ncstate

    ncstate Guest

    Thanks for the help. the only thing that i seemed to find that worked
    was to #include <iostream.h> and it gives me a depreciation message but
    compiles and runs fine. is this normal for g++ 3.2.2?? it compiles fine
    with #include <iostream> with g++ 2.8.1? should i just keep using the .h
    extention and ignore the message?


    --
    Posted via http://dbforums.com
     
    ncstate, Oct 24, 2003
    #9
  10. ncstate

    Artie Gold Guest

    ncstate wrote:
    > Thanks for the help. the only thing that i seemed to find that worked
    > was to #include <iostream.h> and it gives me a depreciation message but
    > compiles and runs fine. is this normal for g++ 3.2.2?? it compiles fine
    > with #include <iostream> with g++ 2.8.1? should i just keep using the .h
    > extention and ignore the message?
    >

    No, you should use <iostream> -- which is the proper header -- and
    correct your *code*.

    HTH,
    --ag
    --
    Artie Gold -- Austin, Texas
    Oh, for the good old days of regular old SPAM.
     
    Artie Gold, Oct 24, 2003
    #10
  11. ncstate

    Mike Wahler Guest

    "ncstate" <> wrote in message
    news:...
    >
    > Thanks for the help. the only thing that i seemed to find that worked
    > was to #include <iostream.h> and it gives me a depreciation message but
    > compiles and runs fine. is this normal for g++ 3.2.2?? it compiles fine
    > with #include <iostream> with g++ 2.8.1? should i just keep using the .h
    > extention and ignore the message?


    No.

    #include <iostream>

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

    -Mike
     
    Mike Wahler, Oct 24, 2003
    #11
  12. ncstate

    David Rubin Guest

    Mike Wahler wrote:
    [snip]
    >>I found this
    >>diagnostic a bit confusing too the first time I saw it.

    >
    > "'cout' undeclared." Exactly the case. How much more specific
    > could it be? :)
    >
    > Before you say e.g. "well the compiler should 'know' it's a
    > 'namespace std' issue, I'll say that no, it cannot (and should not)
    > make such an assumption.


    I wasn't suggesting that.

    > OP's problem is not an 'ambiguous' error message, but insufficient
    > background knowledge before using the compiler.


    This is more like it. After years of not using std::, I am sometimes
    bewildered when the compiler can't find declarations I know
    exist...somewhere.

    /david

    --
    "As a scientist, Throckmorton knew that if he were ever to break wind in
    the echo chamber, he would never hear the end of it."
     
    David Rubin, Oct 24, 2003
    #12
  13. ncstate

    ncstate Guest

    Thanks for all the help.. Sorry I didn't catch on before. All of the
    posts were helpful and appriciated.



    Jeff


    --
    Posted via http://dbforums.com
     
    ncstate, Oct 24, 2003
    #13
  14. ncstate

    MPBroida Guest

    Mike Wahler wrote:
    >
    > "MPBroida" <michael.p.broida@boeing_oops.com> wrote in message
    > news:3F9858AB.2DF20187@boeing_oops.com...
    > > ncstate wrote:
    > > >
    > > > 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. i found the
    > > > header file in /usr/include/c++/3.2.2/ so that is why it is included
    > > > like that. suggestions?
    > > >
    > > > I'm running red hat 9
    > > >
    > > > code :
    > > >
    > > > #include </usr/include/c++/3.2.2/iostream>
    > > >

    > >
    > > It's been a little while, but I seem to recall that
    > > including a file from the "builtin" include dirs is
    > > done via:
    > > #include <filename>
    > > That tells the compiler to use its own include path(s).
    > >
    > > But including a USER file is done via:
    > > #include "filename"
    > > That tells the compiler to use the include paths from
    > > the compile commandline (or defaults)(or project/etc).
    > >
    > > Try changing the < and > to " and " and see what
    > > happens.

    >
    > Please check your facts before making such suggestions.


    Just looked in my "fact sack" and all looks
    well. What facts are you unhappy with?

    Mike
     
    MPBroida, Oct 24, 2003
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lans Redmond

    iostream::read function

    Lans Redmond, Jun 26, 2003, in forum: C++
    Replies:
    0
    Views:
    1,159
    Lans Redmond
    Jun 26, 2003
  2. John Tiger
    Replies:
    10
    Views:
    5,608
  3. ai@work
    Replies:
    9
    Views:
    548
    Ron Natalie
    Dec 16, 2004
  4. S. Nurbe

    iostream + iostream.h

    S. Nurbe, Jan 14, 2005, in forum: C++
    Replies:
    7
    Views:
    780
    red floyd
    Jan 15, 2005
  5. red floyd
    Replies:
    3
    Views:
    537
    Dietmar Kuehl
    Mar 8, 2005
Loading...

Share This Page