istream_iterator and ostream_iterator problem

Discussion in 'C++' started by Chris Mantoulidis, Dec 14, 2003.

  1. Hello all... I get an error message for the following program by the
    compiler:

    #include <iostream>
    #include <string>

    using namespace std;

    ostream_iterator<string> oo(cout);

    int main()
    {
    *oo = "Hello ";
    ++oo;
    *oo = "world!\n";

    return 0;
    }

    the error says: "ostream_iterator is used as a type, but is not
    defined as a type."... What can I do to fix that?? I get the same
    error with istream_iterator...

    cmad
     
    Chris Mantoulidis, Dec 14, 2003
    #1
    1. Advertising

  2. Chris Mantoulidis wrote in news:a8587dd9.0312132333.526171b8
    @posting.google.com:

    > Hello all... I get an error message for the following program by the
    > compiler:
    >
    > #include <iostream>
    > #include <string>
    >


    #include <iterator>

    > using namespace std;
    >
    > ostream_iterator<string> oo(cout);
    >
    > int main()
    > {
    > *oo = "Hello ";
    > ++oo;
    > *oo = "world!\n";
    >
    > return 0;
    > }
    >
    > the error says: "ostream_iterator is used as a type, but is not
    > defined as a type."... What can I do to fix that?? I get the same
    > error with istream_iterator...
    >


    std::eek:stream_iterator is declared in <iterator>.

    HTH

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Dec 14, 2003
    #2
    1. Advertising

  3. Chris Mantoulidis

    Jord Guest

    >>>>> ">>" == Chris Mantoulidis <> writes:

    >>> Hello all... I get an error message for the following program by the
    >>> compiler:


    >>> #include <iostream>
    >>> #include <string>
    >>>
    >>> using namespace std;
    >>>
    >>> ostream_iterator<string> oo(cout);
    >>> int main()
    >>> {
    >>> *oo = "Hello ";
    >>> ++oo;
    >>> *oo = "world!\n";
    >>> return 0;
    >>> }


    >>> the error says: "ostream_iterator is used as a type, but is not
    >>> defined as a type."... What can I do to fix that?? I get the same
    >>> error with istream_iterator...


    >>> cmad


    Chris,

    You need to include <iterator>!

    ostream_iterator exists as a unique template class as compared to the
    iterators of STL containers which use traits to create their own nested
    iterator classes, hence the scope resolution operator "::" when these are
    instantiated, eg.

    vector<int> my_vec(10);
    // ...
    vector<int>::iterator my_vec_itor = my_vec.begin();

    .... you could even use a little more of the STL:

    #include <iostream>
    #include <iterator>
    #include <string>
    #include <vector>

    using namespace std;

    int main()
    {
    vector<string> oo_vec;
    oo_vec.push_back("Hello ");
    oo_vec.push_back("world!\n");
    copy(oo_vec.begin(), oo_vec.end(), ostream_iterator<string>(cout));

    return 0;
    }

    J.
    --
     
    Jord, Dec 15, 2003
    #3
    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. dragoncoder

    Problem with std::istream_iterator

    dragoncoder, Sep 18, 2006, in forum: C++
    Replies:
    8
    Views:
    516
    Jerry Coffin
    Sep 18, 2006
  2. Pradeep
    Replies:
    5
    Views:
    1,114
    =?ISO-8859-1?Q?Jens_M=FCller?=
    Oct 19, 2006
  3. Replies:
    2
    Views:
    489
    Pete Becker
    Nov 13, 2006
  4. Replies:
    5
    Views:
    475
    Sylvester Hesp
    May 30, 2007
  5. Replies:
    8
    Views:
    491
    James Kanze
    Sep 19, 2011
Loading...

Share This Page