Thread: ostream_iterator

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    10

    ostream_iterator

    I have a code snippet given by my instructor in data structures.

    Code:
    template<class T>
    void myDataStruct<T>::output(ostream& out) const
    {// Put the list into the stream out.
       copy(element, element + listSize, ostream_iterator<T>(cout, "  "));
    }
    
    // operator overload <<
    template <class T>
    ostream& operator<<(ostream& out, const myDataStruct<T>& x)
       {x.output(out); return out;}
    when I compile, I get these errors:

    myDataStruct.h: In member function ‘T& myDataStruct<T>::get(int) const’:
    myDataStruct.h:77: error: expected primary-expression before ‘&’ token
    myDataStruct.h: In member function ‘void myDataStruct<T>:utput(std:stream&) const’:
    myDataStruct.h:138: error: ‘ostream_iterator’ was not declared in this scope
    myDataStruct.h:138: error: expected primary-expression before ‘>’ token


    I don't get what's going on... this should work "out of the box" sortof speak... at least i'd expect that form my porfessor.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Fire your professor...

    Soma

    Code:
    template<class T>
    void myDataStruct<T>::output(std::ostream& out) const
    {// Put the list into the stream out.
       using namespace std;
       const char separator[] = {out.fill(), '\0'};
       // and really you should use a `std::ostream::sentry' object
       copy(element, element + listSize, ostream_iterator<T>(out, separator));
    }
    
    // operator overload <<
    template <class T>
    std::ostream& operator<<(std::ostream& out, const myDataStruct<T>& x)
       {x.output(out); return out;}

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    o wow..
    I'm still getting:

    Code:
    In file included from myDataStruct.cpp:12:
    myDataStruct.h: In member function ‘void myDataStruct<T>::output(std::ostream&) const’:
    myDataStruct.h:140: error: ‘ostream_iterator’ was not declared in this scope
    myDataStruct.h:140: error: expected primary-expression before ‘>’ token

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    o_O

    Seriously, you need a better teacher.

    Soma

    ostream_iterator

  5. #5
    Registered User
    Join Date
    Sep 2010
    Posts
    10
    ujhahahahahhahh!!! how did you do that??

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    myDataStruct.h: In member function ‘void myDataStruct<T>:utput(std:stream&) const’:
    myDataStruct.h:138: error: ‘ostream_iterator’ was not declared in this scope
    myDataStruct.h:138: error: expected primary-expression before ‘>’ token
    That could be caused by not having the <iterator> header included.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    46
    DOOOHHHH!!!!!! As homer would say!

    indeed, i had forgotten to #include <iterator>

Popular pages Recent additions subscribe to a feed