istream_iterator and ostream_iterator problem

C

Chris Mantoulidis

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
 
R

Rob Williscroft

Chris Mantoulidis wrote in @posting.google.com:
Hello all... I get an error message for the following program by the
compiler:

#include <iostream>
#include <string>

#include said:
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.
 
J

Jord

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.
--
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top