Embarrassing problem with vector
I am teaching myself C++ and I am reading Lippman's book "Essential C+
+." I wrote a simple program that uses vectors. Here it is (the line
numbers are there for convenience, duh), I disclaim all copyright.
1 #include <iostream>
2 #include <vector>
3
4 using namespace std;
5
6 template<typename T>
7 void PrintVector(const vector<T> &vec)
8 {
9 vector<T>::const_iterator cur = vec.begin();
10 while (cur != vec.end()) {
11 cout << *cur << ", ";
12 ++cur;
13 }
14 cout << endl;
15 }
16
17 int main()
18 {
19 vector<int> vec;
20 for (int i = 0; i < 32; ++i) {
21 vec.push_back(i);
22 }
23 PrintVector(vec);
24 }
My compiler refuses to compile it, saying
vectest.cc: In function =91void PrintVector(const std::vector<T,
std::allocator<_CharT> >&)':
vectest.cc:9: error: expected `;' before =91cur'
vectest.cc:10: error: =91cur' was not declared in this scope
vectest.cc: In function =91void PrintVector(const std::vector<T,
std::allocator<_CharT> >&) [with T = int]':
vectest.cc:23: instantiated from here
vectest.cc:9: error: dependent-name =91std::vector::const_iterator' is
parsed as a non-type, but instantiation yields a type
vectest.cc:9: note: say =91typename std::vector::const_iterator' if a
type is meant
I thought I did everything by the book, but seems so I didn't. :-/
What is the problem with this code? I pretty sure the problem lies on
the line 9...
Tuomas