Topic: Problem with 'std::sort


Author: Jan Falke <i21ajanf@unibw-muenchen.de>
Date: 2000/01/13
Raw View
hi,

i'm going nuts. i tried to sort a standard list with 'sort' but always get
this
error:

error C2664: 'void __cdecl stlport::sort(struct
stlport::_List_iterator<struct CRenameFiles2Dlg::file_struct,struct
stlport::_Nonconst_traits<struct CRenameFiles2Dlg::file_struct> >
,struct stlport::_List_iterator<struct CRenameFiles2Dlg::file_struct,struct
stlport::_Nonconst_traits<struct CRenameFiles2Dlg::file_struct> >,bool
(__thiscall *)(const struct CRenameFiles2Dlg::file_struct,struct
CRenameFiles2Dlg::file_struct))' :
Conversion of parameters 3 from
'bool               (const struct CRenameFiles2Dlg::file_struct,struct
CRenameFiles2Dlg::file_struct)' to
'bool (__thiscall *)(const struct CRenameFiles2Dlg::file_struct,struct
CRenameFiles2Dlg::file_struct)' is not possible


what's wrong??

thx for your time,

jan


////////////////////////////////////////////////////////////////////////////
////

*.HPP:

// ...

using namespace stlport;

class CRenameFiles2Dlg : public CDialog
{
public:

    struct file_struct {
        string path;
        string old_name;
        string new_name;
    };

    list<file_struct> filename_list;

protected:

    bool ListObj_lt( const file_struct x,  file_struct y );
};

////////////////////////////////////////////////////////////////////////////
////

*.CPP:

void CRenameFiles2Dlg::OnPreview()
{
    // ...

    sort( filename_list.begin(), filename_list.end(), ListObj_lt );

    // ...
}



bool CRenameFiles2Dlg::ListObj_lt( const file_struct x, const file_struct
y )
{
    // ...

    return( true );
}

////////////////////////////////////////////////////////////////////////////
////






---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: =?ISO-8859-1?Q?J=F6rg?= Barfurth <joerg.barfurth@germany.sun.com>
Date: 2000/01/13
Raw View
A non-static member function wont work as predicate for an STL=20
algorithm. Try to declare it as static.

HTH
- J=F6rg

>>>>>>>>>>>>>>>>>> Urspr=FCngliche Nachricht <<<<<<<<<<<<<<<<<<

Am 13.01.00, 23:45:45, schrieb Jan Falke <i21ajanf@unibw-muenchen.de>

zum Thema Problem with 'std::sort':


> hi,

> i'm going nuts. i tried to sort a standard list with 'sort' but always get
> this error:

> Conversion of parameters 3 from
> 'bool               (const struct CRenameFiles2Dlg::file_struct,struct
> CRenameFiles2Dlg::file_struct)' to
> 'bool (__thiscall *)(const struct CRenameFiles2Dlg::file_struct,struct
> CRenameFiles2Dlg::file_struct)' is not possible



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Niklas Mellin <nimel@my-deja.com>
Date: 2000/01/14
Raw View
In article <387c810b.0@news.unibw-muenchen.de>,
  Jan Falke <i21ajanf@unibw-muenchen.de> wrote:

> i'm going nuts. i tried to sort a standard list with 'sort' but
> always get this error:
>
> error C2664: ...

[simplified code:]

list<SomeType> theList;
sort(theList.begin(), theList.end(), pred);

The reason you get that error is that sort requiers
random access iterators, and list can only supply you with
bidirectional iterators. Instead use the sort member function
of list, or switch to another container type that are supplies
random access iterators:

theList.sort(pred);

or

vector<SomeType> theVector;  // or deque, or C array
sort(theVector.begin(), theVector.end(), pred);

In your case with a list of filenames I guess it really
doesn't matter which solution you choose, since both
the elements themselves and the number of elements that
are to be sorted are probably quite small.

In the general case you can expect std::sort to be faster
if you have a large number of small elements, and
std::list::sort to be faster if you have a small number of
large elements.

/Niklas Mellin



Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jan Falke" <alatar@alatar.de>
Date: 2000/01/14
Raw View
thank you j   rg and niklas.

a combination of your advices succeded:

j   rg wrote:
>A non-static member function wont work as predicate for an STL
>algorithm. Try to declare it as static.

right, my member-function needed the keyword 'static'

      static bool ListObj_lt( const file_struct x, const file_struct y );

niklas wrote:
>Instead use the sort member function
>of list [...]
>theList.sort(pred);

      filename_list.sort( ListObj_lt );

thx again,
jan





"Niklas Mellin" <nimel@my-deja.com> wrote in message
news:85kurm$5j1$1@nnrp1.deja.com...
> In article <387c810b.0@news.unibw-muenchen.de>,
>   Jan Falke <i21ajanf@unibw-muenchen.de> wrote:
>
> > i'm going nuts. i tried to sort a standard list with 'sort' but
> > always get this error:
> >
> > error C2664: ...
>
> [simplified code:]
>
> list<SomeType> theList;
> sort(theList.begin(), theList.end(), pred);
>
> The reason you get that error is that sort requiers
> random access iterators, and list can only supply you with
> bidirectional iterators. Instead use the sort member function
> of list, or switch to another container type that are supplies
> random access iterators:
>
> theList.sort(pred);
>
> or
>
> vector<SomeType> theVector;  // or deque, or C array
> sort(theVector.begin(), theVector.end(), pred);
>
> In your case with a list of filenames I guess it really
> doesn't matter which solution you choose, since both
> the elements themselves and the number of elements that
> are to be sorted are probably quite small.
>
> In the general case you can expect std::sort to be faster
> if you have a large number of small elements, and
> std::list::sort to be faster if you have a small number of
> large elements.
>
> /Niklas Mellin
>
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]
>


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]