T
Tim Partridge
I have a std list of student objects and I would like to sort them by
providing a comparison function. I have gotten it to work if my function
is global, but I would like to encapsulate it in a class. For example, my
attempt below fails, but if I move sortFunctor out of the class, it will
compile.
#include <list>
using namespace std;
class foo {
public:
foo(int i) : i(i) {};
~foo() {};
int i;
// compiles if this in the global namespace
bool sortFunctor(const foo &f1, const foo &f2) {
return (f1.i < f2.i);
}
};
int main() {
list< foo > l;
foo f1(8);
foo f2(3);
l.push_back(f1);
l.push_back(f2);
// compiles if foo:: is removed
l.sort(foo::sortFunctor);
return 0;
}
Regards,
Tim Partridge
providing a comparison function. I have gotten it to work if my function
is global, but I would like to encapsulate it in a class. For example, my
attempt below fails, but if I move sortFunctor out of the class, it will
compile.
#include <list>
using namespace std;
class foo {
public:
foo(int i) : i(i) {};
~foo() {};
int i;
// compiles if this in the global namespace
bool sortFunctor(const foo &f1, const foo &f2) {
return (f1.i < f2.i);
}
};
int main() {
list< foo > l;
foo f1(8);
foo f2(3);
l.push_back(f1);
l.push_back(f2);
// compiles if foo:: is removed
l.sort(foo::sortFunctor);
return 0;
}
Regards,
Tim Partridge