Issue with iterators with stl set using a custom comparator

Hello,
I'm trying to make a custom set that will only keep the highest n entries based on a custom comparator (if there is already a way of doing this with the STL let me know!). Anyway, it works on Windows with Visual Studio 2010, but not on Linux with GCC 4.1.2. My code is as follows:

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
#include <set>
#include <iostream>

//typedef double T;

template<class T>
bool test_comp(T a, T b){
        return a < b; // this is arbitrary to illustrate point
}

template<class T>
class MySet {
private:
        std::set<T,bool(*)(T, T)> _set;
        size_t maxSize;
public:
        MySet(size_t _maxSize, bool(*fpt)(T,T)){
                _set = std::set<T,bool(*)(T,T)>(fpt);
                maxSize = _maxSize;

        }

        bool insert(T d){
                if(_set.size() == maxSize){
                        std::set<T,bool(*)(T,T)>::const_iterator it = --_set.rbegin().base();

                        if(d < *it){
                                _set.erase(it);
                                _set.insert(d);
                                return true;
                        } else {
                                return false;
                        }
                }

                _set.insert(d);
                return false;
        }

        T getOptimal() const {
                return *(_set.begin());
        }
};

int main(int argc, char ** argv){
        //create set with a function pointer comparator
        MySet test(3,test_comp);

        test.insert(2);
        test.insert(3);
        test.insert(4);
        test.insert(5);
        test.insert(6);
        test.insert(1);

        std::cout << test.getOptimal() << std::endl;
        return 0;
}


When this is compiled, the following output is generated:

test.cpp: In member function 'bool MySet<T>::insert(T)':
test.cpp:23: error: expected `;' before 'it'
test.cpp:25: error: 'it' was not declared in this scope
test.cpp: In function 'int main(int, char**)':
test.cpp:45: error: missing template arguments before 'test'
test.cpp:45: error: expected `;' before 'test'
test.cpp:47: error: 'test' was not declared in this scope


If the template<class T> is replaced with typedef double T; it compiles fine. Any ideas?
See here about the first error (read the bit about :Disambiguating Dependent Names)
http://cppreference.com/wiki/keywords/typename

For the second error (in main function)
This is incorrect: MySet test(3,test_comp);
should be MySet<some_type_in_here> test(3,test_comp); as MySet is a template class.
You can do it with the STL.
http://www.cplusplus.com/reference/stl/set/

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
#include <functional>
#include <set>

struct mysetcomp: std::binary_function <int, int, bool>
  {
  bool operator () ( const int& a, const int& b ) const
    {
    return /* does a come before b? for example: */  a < b;
    }
  };

typedef std::set <int, mysetcomp> MySet;

#include <algorithm>
#include <iostream>
#include <iterator>
using namespace std;

int main()
  {
  MySet test;

  test.insert( 2 );
  test.insert( 3 );
  test.insert( 7 );
  test.insert( -5 );
  test.insert( 11 );

  copy( test.begin(), test.end(), ostream_iterator <int> ( cout, " " ) );
  cout << endl;

  return 0;
  }

Hope this helps.
Topic archived. No new replies allowed.