problem compiling functor with sort

J

Joel

I am having some problems compiling my code on Mandrake 10 with g++
(GCC 3.3.2). The problem seems to be in that I try to define a functor
that compares two pointer objects, and use that functor to sort a list
of pointers. It seems correct to me, and when I read on these boards I
could not find any one complaining about the same problem. But my
compiler is giving me error messages. I am hoping that someone can
help to point out where my problem is.

sample code:
--------

#include <list.h>

class F {
public:
F(int a):a(a){}
int getA() {return a;}
private:
int a;
};

struct ltfa
{
bool operator()(F* f1, F* f2) const
{
return f1->getA() < f2->getA();
}
};

int main ( int argc, int argv[] ) {
list<F*> lf;
F* f1 = new F(1);
lf.push_back(f1);
F* f2 = new F(2);
lf.push_back(f2);

sort(lf.begin(), lf.end(), ltfa());
}

---------
error message:

/usr/include/c++/3.3.2/bits/stl_algo.h: In function `void
std::sort(_RandomAccessIter, _RandomAccessIter, _Compare) [with
_RandomAccessIter = std::_List_iterator<F*, F*&, F**>, _Compare =
ltfa]':
test2.cc:26: instantiated from here
/usr/include/c++/3.3.2/bits/stl_algo.h:2211: error: no match for
'operator-' in '__last - __first'
/usr/include/c++/3.3.2/bits/stl_algo.h: In function `void
std::__final_insertion_sort(_RandomAccessIter, _RandomAccessIter,
_Compare) [with _RandomAccessIter = std::_List_iterator<F*, F*&, F**>,
_Compare = ltfa]':
/usr/include/c++/3.3.2/bits/stl_algo.h:2212: instantiated from `void
std::sort(_RandomAccessIter, _RandomAccessIter, _Compare) [with
_RandomAccessIter = std::_List_iterator<F*, F*&, F**>, _Compare =
ltfa]'
test2.cc:26: instantiated from here
/usr/include/c++/3.3.2/bits/stl_algo.h:2076: error: no match for
'operator-' in '__last - __first'
/usr/include/c++/3.3.2/bits/stl_algo.h:2077: error: no match for
'operator+' in '__first + _M_threshold'
/usr/include/c++/3.3.2/bits/stl_algo.h:2078: error: no match for
'operator+' in '__first + _M_threshold'
/usr/include/c++/3.3.2/bits/stl_algo.h: In function `void
std::__insertion_sort(_RandomAccessIter, _RandomAccessIter, _Compare)
[with _RandomAccessIter = std::_List_iterator<F*, F*&, F**>, _Compare
= ltfa]':
/usr/include/c++/3.3.2/bits/stl_algo.h:2081: instantiated from `void
std::__final_insertion_sort(_RandomAccessIter, _RandomAccessIter,
_Compare) [with _RandomAccessIter = std::_List_iterator<F*, F*&, F**>,
_Compare = ltfa]'
/usr/include/c++/3.3.2/bits/stl_algo.h:2212: instantiated from `void
std::sort(_RandomAccessIter, _RandomAccessIter, _Compare) [with
_RandomAccessIter = std::_List_iterator<F*, F*&, F**>, _Compare =
ltfa]'
test2.cc:26: instantiated from here
/usr/include/c++/3.3.2/bits/stl_algo.h:2006: error: no match for
'operator+' in '__first + 1'

...... etc.

It is long, if someone needs to see the complete message, let me know
and I will post it.

Thank you very much for your help!

Joel
 
J

John Harrison

Joel said:
I am having some problems compiling my code on Mandrake 10 with g++
(GCC 3.3.2). The problem seems to be in that I try to define a functor
that compares two pointer objects, and use that functor to sort a list
of pointers. It seems correct to me, and when I read on these boards I
could not find any one complaining about the same problem. But my
compiler is giving me error messages. I am hoping that someone can
help to point out where my problem is.

sample code:
--------

#include <list.h>

class F {
public:
F(int a):a(a){}
int getA() {return a;}
private:
int a;
};

struct ltfa
{
bool operator()(F* f1, F* f2) const
{
return f1->getA() < f2->getA();
}
};

int main ( int argc, int argv[] ) {
list<F*> lf;
F* f1 = new F(1);
lf.push_back(f1);
F* f2 = new F(2);
lf.push_back(f2);

sort(lf.begin(), lf.end,ltfa
}

---------
error message:


It is long, if someone needs to see the complete message, let me know
and I will post it.

Thank you very much for your help!

Joel

Actually the problem is that you cannot use std::sort with a std::list.
std::sort needs random access iterators but std::list only has bidirectional
iterators.

std::list has its own sort method, which is slightly less efficient than
std::sort. Try this

lf.sort(ltfa());

john
 
J

Joel

Actually the problem is that you cannot use std::sort with a std::list.
std::sort needs random access iterators but std::list only has bidirectional
iterators.

std::list has its own sort method, which is slightly less efficient than
std::sort. Try this

lf.sort(ltfa());

john

Thank you!! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top