Jan 4, 2011 at 3:49pm UTC
I am trying to use the Standard Template Library linked lists for the first time and I hit a problem with sorting. I need a table with two columns and an arbitrary number of rows, so I made a class that has a private data member which is a list of lists.
1 2 3 4 5 6
class Table
{
...
private :
std::list< list< double > > myTable;
}
This has worked out just fine, until I got to sorting. I'm trying to sort the rows (the inner lists) in the table (the outer list) by a specified callback function and it's not compiling for me.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
void Table::sort( void )
{
myTable.sort( compFunc );
}
bool Table::compFunc( list< double > first, list< double > last )
{
list< double >::iterator dataPos = first.begin();
double firstData = *dataPos;
dataPos = second.begin();
double secondData = *dataPos;
return firstData < secondData;
}
The compiler is giving me the following error:
error: no matching function for call to 'std::list<std::list<double, std::allocator<double> >,
std::allocator<std::list<double, std::allocator<double> > > >::sort(<unresolved overloaded function type>)'
C:\nburn\gcc-m68k\m68k-elf\include\c++\4.2.1/bits/list.tcc:271: note: candidates are: void std::list<_Tp, _Alloc>::sort()
[with _Tp = std::list<double, std::allocator<double> >, _Alloc = std::allocator<std::list<double, std::allocator<double> > >]
C:\nburn\gcc-m68k\m68k-elf\include\c++\4.2.1/bits/list.tcc:348: note: void std::list<_Tp, _Alloc>::sort(_StrictWeakOrdering)
[with _StrictWeakOrdering = bool (Table::*)(std::list<double, std::allocator<double> >, std::list<double, std::allocator<double> >),
_Tp = std::list<double, std::allocator<double> >, _Alloc = std::allocator<std::list<double, std::allocator<double> > >]
Any ideas about what I'm doing wrong?
Thanks.
Last edited on Jan 4, 2011 at 3:50pm UTC
Jan 4, 2011 at 4:23pm UTC
Is compFunc declared static ?
Jan 4, 2011 at 4:58pm UTC
The idea is that it could be called comp(A, B);
(if is not static you will need an object to perform the call)
Also, avoid copying your lists, and pass them as constant references
bool Table::compFunc(const std::list< double > &first,const std::list< double > &last )
Jan 4, 2011 at 5:23pm UTC
Oh, that makes sense. Thanks for the help, looks like it's working now.