having problem compiling example in "Problem Solving in C++" book

I typed it in word with the intent to later modify and play around with the code, but I cannot get it to complile using GCC. Any pointers?

Error received is:
Code:
ione.cpp:19: error: ‘iterator’ was not declared in this scope

Makefile:
Code:
ione : ione.cpp
	g++ -o ione ione.cpp

Code:
#include <iostream>
#include <cstdlib>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::vector<int>::iterator;


int main( int argc, char* argv[] ) 
{
  vector<int> container;

  for (int i = 1; i <= 4; i++)
    container.push_back(i);

  cout << "Here is what is in the container: \n";

  iterator p;
  for (p = container.begin(); p != container.end(); p++)
    cout << *p << " ";
  cout << endl;

  cout << "Setting entries to 0:\n";
  for (p= container.begin(); p != container.end(); p++)
    *p = 0;

  cout << "Container now contains:\n";
  for (p = container.begin(); p != container.end(); p++)
    cout << *p << " ";

  exit(0);
}
 
Hey I'm on gcc 4.4.4. I think that this could be a version issue. I typed it into the book, and the library on my system does not contain the
Code:
std::vector<int>::iterator
class for me to use is my guess.
 
The using std::vector<int>::iterator; line is not valid c++ because the template can't be instantiated (I think that's the term, I haven't used c++ in a while) in a using declaration. It's also not needed because the previous line already brings in the needed definition of the iterator.

Change the line with
Code:
iterator p;

To:
Code:
vector<int>::iterator p;
 
Thank you. The reason I had it like that is because the book had it written like that. Programming with compiled languages involves many caveats.
 
Back
Top