PDA

View Full Version : Question about read-only object.



robgeek
8th April 2015, 00:53
Good evening!

Why i'm getting the following error message? How can i fix it?

QList<Number> Historic::descendingOrder(QList<Number> ranking)
{
Number aux(0, 0);
int length = ranking.size( );

for(int i = 0; i < length; i++)
{
for(int j = 0; j < (length - 1); j++)
{
if(ranking.at( j ).acum < ranking.at(j + 1).acum)
{
aux.val = ranking.at(j + 1).val;
aux.acum = ranking.at(j + 1).acum;
ranking.at(j + 1).val = ranking.at( j ).val; //Error message here
ranking.at(j + 1).acum = ranking.at( j ).acum; //Error message here
ranking.at( j ).val = aux.val; //Error message here
ranking.at( j ).acum = aux.acum; //Error message here
}
}
}

return ranking;
}


D:\Programming\C-C++\Qt\Windows\historic.cpp:121: error: assignment of member 'Number::val' in read-only object
ranking.at(j + 1).val = ranking.at( j ).val;

Everything in "Number" class is public. I don't understand!

wysota
8th April 2015, 08:55
at() returns a const object, you cannot assign to it. Use the index operator instead.

robgeek
8th April 2015, 14:55
at() returns a const object, you cannot assign to it. Use the index operator instead.
What do you think about using [ ] instead of indexOf() or at() with QList and QVector?

anda_skoa
8th April 2015, 15:23
I think what you are really looking for is a generic sorting algorithm, like std::sort() or qSort()

Cheers,
_