STL List sorting

Started by
2 comments, last by beebs1 16 years, 11 months ago
Hiya there, I'm having trouble getting my code to compile. I declare a list as std::list<float> m_sampleList;, and then try to call it's sort() method. The compiler complains that C2662: 'void std::list<_Ty>::sort(void)' : cannot convert 'this' pointer from 'const std::list<_Ty>' to 'std::list<_Ty> &' - with _Ty=float - Conversion loses qualifiers.. I have to admit I have no idea what that means. I'm not entirely sure what's going on, the code looks fine to me.

private:

    std::list<float>	m_sampleList;

// and later...

m_sampleList.sort();
Any help would be much appreciated, thanks [smile]
Advertisement
Can we see the 'later' part? The error means that you are trying to perform a non-const operation on a const object. You would either need to make sure the object is not const, or declare m_sampleList mutable, which I can't recommend without knowing more.
It's hard to be certain from the limited code you've shown, but my best guess would be that you're calling the sort function from within a const member function. Sorting a list is, obviously, mutative and therefore the list (or the function it's called from cannot be const.

ie. I'm going to guess that surrounding the code you've quoted is something like this:

void YourClass::SomeFunction() const
{
m_sampleList.sort();
}

...so you either need to make the function non-const, or the list mutable, or rethink why you're trying to change something from within a const function.

If this isn't the case, we're going to need more code...
That's exactly what it was. I knew it had to be a stupid mistake like that... (Kicks self)

Seriously though, thanks :)

This topic is closed to new replies.

Advertisement