newbie iterator question...

  • Thread starter =?ISO-8859-1?Q?Martin_J=F8rgensen?=
  • Start date
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Hi,

I have this book "Objected-oriented programming in C++", 4.th edition by
Robert Lafore... It says on the back of this book that he has written
books about programming since 1982... Yeah, right... I did that too.

The whole chapter 15 about STL is completely full of errors. I get a lot
of compiler errors/warnings when I work with iterators... I just tried 4
examples from his chapter 15 about the STL...

I also have this "accelerated c++" but now I want to know why this
doesn't work:

// initer.cpp
// demonstrates istream_iterator
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

int main()
{
list<float> fList(5); // uninitialized list

cout << "\nEnter 5 floating-point numbers: ";
// istream iterators
istream_iterator<float> cin_iter(cin); // cin //<- ERROR
istream_iterator<float> end_of_stream; // eos //<- ERROR
// copy from cin to fList
copy( cin_iter, end_of_stream, fList.begin() ); //<- ERROR

cout << endl; // display fList
ostream_iterator<float> ositer(cout, "--"); //<- ERROR
copy(fList.begin(), fList.end(), ositer); //<- ERROR
cout << endl;
return 0;
}


5 lines with errors:

mac$ g++ initer.cpp
initer.cpp: In function 'int main()':
initer.cpp:14: error: 'istream_iterator' was not declared in this scope
initer.cpp:14: error: expected primary-expression before 'float'
initer.cpp:14: error: expected `;' before 'float'
initer.cpp:15: error: expected primary-expression before 'float'
initer.cpp:15: error: expected `;' before 'float'
initer.cpp:17: error: 'cin_iter' was not declared in this scope
initer.cpp:17: error: 'end_of_stream' was not declared in this scope
initer.cpp:20: error: 'ostream_iterator' was not declared in this scope
initer.cpp:20: error: expected primary-expression before 'float'
initer.cpp:20: error: expected `;' before 'float'
initer.cpp:21: error: 'ositer' was not declared in this scope


Compiler is g++. I haven't used iterators before but if I just knew how
to deal with this, I could probably correct the other wrong example code
that also deals with iterators, in the rest of chapter 15 in this lousy
book.


Best regards / Med venlig hilsen
Martin Jørgensen
 
M

Mark P

Martin said:
Hi,

I have this book "Objected-oriented programming in C++", 4.th edition by
Robert Lafore... It says on the back of this book that he has written
books about programming since 1982... Yeah, right... I did that too.

The whole chapter 15 about STL is completely full of errors. I get a lot
of compiler errors/warnings when I work with iterators... I just tried 4
examples from his chapter 15 about the STL...

I also have this "accelerated c++" but now I want to know why this
doesn't work:

// initer.cpp
// demonstrates istream_iterator
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;

Try adding:
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Mark said:
Try adding:

#include <iterator>

Thanks... I still wonder about how an author apparantly must expect
beginners to figure that out... There are several places where this
#include <iterator> is missing so the program code doesn't compile...


Best regards / Med venlig hilsen
Martin Jørgensen
 
J

Jonathan Mcdougall

Martin said:
Thanks... I still wonder about how an author apparantly must expect
beginners to figure that out...

He probably didn't. Since C++ doesn't specify how standard headers
include each other, it is perfectly possible that this example compiled
fine on a specific compiler. For example, <algorithm> could include
<iterator>, so unless you know exactly what header is needed for a
name, you may run into this problem yourself. The C++ Hello World
example is also a well-known example of this [it's not a bug it's a]
feature.


Jonathan
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Jonathan said:
Martin said:
Thanks... I still wonder about how an author apparantly must expect
beginners to figure that out...


He probably didn't. Since C++ doesn't specify how standard headers
include each other, it is perfectly possible that this example compiled
fine on a specific compiler. For example, <algorithm> could include
<iterator>, so unless you know exactly what header is needed for a
name, you may run into this problem yourself. The C++ Hello World
example is also a well-known example of this [it's not a bug it's a]
feature.

A feature? Sounds stupid. Okay, but I would be more glad if the author
wrote that g++ users should write #include <iterator> if it doesn't
work. But at least I know how to deal with the problem now.


Best regards / Med venlig hilsen
Martin Jørgensen
 
A

Andrew Koenig

Thanks... I still wonder about how an author apparantly must expect
beginners to figure that out... There are several places where this
#include <iterator> is missing so the program code doesn't compile...

Well, if you're talking about Accelerated C++, it says near the top of page
151 that the stream iterator types are defined in <iterator>.
 
?

=?ISO-8859-1?Q?Martin_J=F8rgensen?=

Andrew said:
Well, if you're talking about Accelerated C++, it says near the top of page
151 that the stream iterator types are defined in <iterator>.

I'm not talking about that book.


Best regards / Med venlig hilsen
Martin Jørgensen
 

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

Forum statistics

Threads
473,780
Messages
2,569,609
Members
45,254
Latest member
Top Crypto TwitterChannel

Latest Threads

Top