STL Linked Lists, Sorting Problem

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
Is compFunc declared static ?
It is not. Should it be?
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 )

Oh, that makes sense. Thanks for the help, looks like it's working now.
Topic archived. No new replies allowed.