-
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>::output(std::ostream&) 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.
-
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;}
-
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
-
o_O
Seriously, you need a better teacher.
Soma
ostream_iterator
-
ujhahahahahhahh!!! how did you do that??
-
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.
-
DOOOHHHH!!!!!! As homer would say!
indeed, i had forgotten to #include <iterator>