help me with iterator declaration!

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  1 #include <iostream>
  2 #include <string>
  3 #include <vector>
  4 #include <sstream>
  5 #include <utility>
  6 using namespace std;
  7
  8 template<typename KeyType, typename InfoType>
  9 class Dictionary
 10 {
 11 private:
 12     vector< pair<KeyType, InfoType> > _store;
 13
 14     int find( KeyType key )
 15     {
 16         for (unsigned int i = 0; i < _store.size(); i++)
 17             if (_store[i].first == key )
 18                 return i;
 19         return -1;
 20     }
 21
 22 public:
 23     Dictionary(){}
 24
 25     void add( KeyType key, InfoType info )
 26     {
 27         int idx = find( key );
 28
 29         if (idx == -1)
 30             _store.push_back( pair<KeyType, InfoType> (key, info ));
 31     }
 32
 33     bool remove(KeyType key){
 34         int idx = find(key);
 35         if (idx == -1)
 36             return false;
 37
 38         vector< pair<KeyType, InfoType> >::iterator it = _store.begin();
 39         it += idx;
 40         _store.erase(it);
 41         return true;
 42     }
 43
 44     void clear(){
 45         vector< pair<KeyType, InfoType> >::iterator it = _store.begin();
 46         while (!_store.isEmpty())
 47             it = _store.erase(it);
 48     }
 49
 50     bool lookup( KeyType key, InfoType& returnInfo )
 51     {
 52         int idx = find( key );
 53         if (idx == -1)
 54             return false;
 55
 56         returnInfo = _store[idx].second;
 57         return true;
 58     }
 59
 60     string toString()
 61     {
 62         ostringstream os;
 63
 64         for (unsigned int i = 0; i < _store.size(); i++){
 65             os << "{" << _store[i].first << ", ";
 66             os << _store[i].second << "}" << endl;
 67         }
 68
 69         return os.str();
 70     }
 71
 72 };

Pls just have a look at the underlined code. Why some compilation errors result form the codes?
P.S.:Dictionary.cpp:38:9: error: need ‘typename’ before ‘std::vector<std::pair<_T1, _T2> >::iterator’ because ‘std::vector<std::pair<_T1, _T2> >’ is a dependent scope
Come on - the compiler is giving you the answer to your problem.
Normally, we use vector<int>::iterator it to declare an iterator to a vector of ints, is there any difference between type int and type pair<t1, t2>?
The difference is that KeyType and InfoType are template parameters, while int is not.

When encountering the expression vector<int>::iterator, the compiler can look into vector<int> and find out that iterator is a type.

When encountering vector<pair<KeyType, InfoType>>::iterator, the compiler does not yet know what kind of KeyType and InfoType this template will be instantiated with. For all it knows, you might provide a specialization of vector<pair<>> for some special KeyType and InfoType, which doesn't have an iterator typedef at all, or one where iterator is a member variable instead, and then you might instantiate this template with those types.

See http://en.cppreference.com/w/cpp/language/dependent_name
Last edited on
cppreference wrote:
1
2
3
4
5
6
7
8
9
10
11
12
template<typename T>
struct S {
    template<typename U> void foo(){}
};
 
template<typename T>
void bar()
{
    S<T> s;
    s.foo<T>(); // error: < parsed as less than operator
    s.template foo<T>(); // OK
}


Heh, heh. The more you know ☆
Topic archived. No new replies allowed.