Getting an error when trying to compile

Write your question here.
I am trying to compile an example program from a book by Walter Savitch and get an error about the iterator and a namespace.
12 25 E:\Savichexamples\SourceCode\Chapter18\18-01.cpp [Error] 'std::vector<int>' is not a namespace
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  Put the code you need help with here.
//DISPLAY 18.1 Iterators Used with a Vector
//Program to demonstrate STL iterators.
#include <iostream>
using namespace std;
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::vector<int>::iterator;

int main( )
{
	using namespace std;
	
    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 << " ";
    cout << endl;

    return 0;
}
In line 22
 
iterator p;

Your declaration of the iterator is wrong. There are two ways to do that

1: Using typedef
Before int main() put:
 
typedef vector<int>::iterator it;

and then in main declare your an iterator
 
it p;

2: Just declaring
This is just declaring the iterator straightforward
 
vector<int>::iterator p;
Last edited on
It worked, Thank you very much!!!!
Topic archived. No new replies allowed.