problem with typedef

Hi there,
in a simple test program I am using typedef to keep things simple, but when I replace the typedef names with the longer original declaration I get errors.
The problem arises when I substitute
pair<MapCode::iterator, MapCode::iterator> range = codes.equal_range(111);
with
pair<const int, string> <multimap<int, string>::iterator, multimap<int, string>::iterator> range = codes.equal_range(718);
I can't substitute the typedef "Pair"
std::pair<const int, std:: string><MapCode::iterator, MapCode::iterator> range = codes.equal_range(111);
without stumbling in an error (i.e. "Expected unqualified-id")
without getting error

I just want to fully understand the logic behind Typedef. I thought it was a simple substitution but these errors make me think otherwise.
Any clarification on the subject is greatly appreciated..

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
#include <iostream>
#include <map>
#include <string>
#include <algorithm>

typedef int Keytype;
typedef std::pair<const Keytype, std:: string> Pair;
typedef std::multimap<Keytype, std::string> MapCode;

int main ()
{
    using namespace std;
    MapCode codes;      //  std::multimap<int, std::string> codes;
    
    codes.insert(Pair(111, "string one"));
    codes.insert(Pair(111, "string two"));
    codes.insert(Pair(112, "string three"));
    
    std::multimap<int, std::string>::iterator it;   //    MapCode::iterator it;
    
    pair<MapCode::iterator, MapCode::iterator> range = codes.equal_range(111);  //<- this works .. the following commented line doesn't 
    //    pair<const int, string> <multimap<int, string>::iterator, multimap<int, string>::iterator> range = codes.equal_range(718);
    cout << "Showing range 111:\n";
    for (it = range.first; it != range.second; it++)
        cout << (*it).second << endl;
    return 0;
}
Last edited on
On line 21 you use pair, but on line 7 you define Pair. Case matters.
It rather perplexes me what you thought line 21 did if you thought 'pair' and 'Pair' were the same type.
Last edited on
hello helios..
thanks! wow what a mistake.. ahah.. After I wrote the code I started substituting the typedef and now I see i got entangled in misinterpreting pair as the Pair typdef (which..yes I have to agree it makes no sense..). well, great, now everything makes sense again :)

Last edited on
Registered users can post here. Sign in or register to post.