Sign in to follow this  
beebs1

STL List sorting

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
That's exactly what it was. I knew it had to be a stupid mistake like that... (Kicks self)

Seriously though, thanks :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this