Results 1 to 4 of 4

Thread: Question about read-only object.

  1. #1
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Question about read-only object.

    Good evening!

    Why i'm getting the following error message? How can i fix it?
    Qt Code:
    1. QList<Number> Historic::descendingOrder(QList<Number> ranking)
    2. {
    3. Number aux(0, 0);
    4. int length = ranking.size( );
    5.  
    6. for(int i = 0; i < length; i++)
    7. {
    8. for(int j = 0; j < (length - 1); j++)
    9. {
    10. if(ranking.at( j ).acum < ranking.at(j + 1).acum)
    11. {
    12. aux.val = ranking.at(j + 1).val;
    13. aux.acum = ranking.at(j + 1).acum;
    14. ranking.at(j + 1).val = ranking.at( j ).val; //Error message here
    15. ranking.at(j + 1).acum = ranking.at( j ).acum; //Error message here
    16. ranking.at( j ).val = aux.val; //Error message here
    17. ranking.at( j ).acum = aux.acum; //Error message here
    18. }
    19. }
    20. }
    21.  
    22. return ranking;
    23. }
    To copy to clipboard, switch view to plain text mode 

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Question about read-only object.

    at() returns a const object, you cannot assign to it. Use the index operator instead.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    robgeek (8th April 2015)

  4. #3
    Join Date
    Mar 2015
    Posts
    105
    Thanks
    50

    Default Re: Question about read-only object.

    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?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Question about read-only object.

    I think what you are really looking for is a generic sorting algorithm, like std::sort() or qSort()

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 23rd March 2015, 00:57
  2. Object Member Question
    By Atomic_Sheep in forum General Programming
    Replies: 1
    Last Post: 15th September 2013, 10:54
  3. Question about initializing an object without assigning...
    By Zingam in forum General Programming
    Replies: 4
    Last Post: 21st August 2013, 21:14
  4. Replies: 10
    Last Post: 17th September 2009, 20:12
  5. Amateur Qt question about movement of an object
    By gtbgmaniak in forum Newbie
    Replies: 1
    Last Post: 19th April 2009, 10:12

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.