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
(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