By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,794 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

istream_iterator and ostream_iterator problem

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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::ostream_iterator is declared in <iterator>.

HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

P: n/a
>>>>> ">>" == Chris Mantoulidis <cm****@yahoo.com> 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.
--
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.