Using char* with a std::map

Hey guys, stuck on a problem hope you guys can help with.

I have a file with some data that I am storing into a map. Part of that data contains a "time" field in the format MM:SS, which I store as a char array and not as a string. (Why am I not using a string? Because using string's inside a struct, and storing this struct in a binary file gives me unwanted problems, so I have resorted to using char arrays.) I want to use this time field as the key for my map, so it can be sorted on it.

After much searching I understand I need to use my own comparison function with the map, otherwise it's gonna sort by the pointer and not by the char array.

So here is a snippet of what I have, and the error I still get.

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
...
struct entrant
{
    long bibno;
    char racetype;
    char lastname[30];
    char firstname[30];
    char sex;
    short int dob_day;
    short int dob_month;
    short int dob_year;
    short int age;
    char teamname[50];
    char teamdivision[50];
    int phone;
    char email[30];
    char address[50];
    char city[50];
    char parish[30];
    char time[10];

} ent;

struct str_cmp
{
    bool operator() (const char* a, const char* b)
    {
        return strcmp(a, b) < 0;
    }
};

map<char*, entrant, str_cmp> mSorted;
map<char*, entrant, str_cmp>::iterator iterSorted;
...


Errors:
1
2
3
4
5
6
7
||In function 'void Report(short int)':|
|32|error: template argument for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map' uses local type 'Report(short int)::str_cmp'|
|32|error:   trying to instantiate 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'|
|32|error: invalid type in declaration before ';' token|
|33|error: template argument for 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map' uses local type 'Report(short int)::str_cmp'|
|33|error:   trying to instantiate 'template<class _Key, class _Tp, class _Compare, class _Alloc> class std::map'|
|33|error: expected initializer before 'iterSorted'|
*Edited line numbers to reflect which line the errors are referring to.

I have no idea what the errors are trying to tell me, as the code seems ok.
Using Codeblocks on Windows 7.
I think that the problem is that you defined your functional object inside a block declarative region. You should place its definition outside the function in which it is defined now.
lol wow. That worked. Didn't know that I needed to have it outside the function. Thanks vlad.
Side note: this changed last year, local types are allowed as template arguments in standard C++ now.
Topic archived. No new replies allowed.