Results 1 to 5 of 5

Thread: passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operator<<(

  1. #1
    Join Date
    Feb 2009
    Location
    Bydgoszcz, Poland
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operator<<(

    Hi ,

    when I compile my code, i have error :

    ERROR Code:
    1. passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operator<<(const T&) [with T = Element]' discards qualifiers
    To copy to clipboard, switch view to plain text mode 

    My class "Element" :

    Element Code:
    1. class Element
    2. {
    3. public:
    4. Element();
    5. Element(QString Aleja,QString Numer,QString Strona);
    6. QString Aleja;
    7. QString Numer;
    8. QString Strona;
    9. };
    To copy to clipboard, switch view to plain text mode 


    And class "Strona" :
    Strona Code:
    1. class Strona
    2. {
    3. public:
    4. Strona();
    5. QList<Element> listElementow;
    6. Element pobierzElement(QString NumerElementu);
    7. Element pobierzElement(int Index);
    8. void dodajElement(QString NrAleji,QString NrElementu,QString NrStrony) const;
    9. };
    To copy to clipboard, switch view to plain text mode 

    and function where i have arror:
    Strona::dodajElement Code:
    1. void Strona::dodajElement(QString NrAleji,QString NrElementu,QString NrStrony) const{
    2. listElementow << Element(NrAleji,NrElementu,NrStrony);
    3. }
    To copy to clipboard, switch view to plain text mode 
    So how a resolved this problem?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operato

    Don't declare dodajElement as const. Then it works. const means that no member will be changed, but you change listElementow. That is the error. If you want your method const and alter a member then use the mutable keyword.

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

    bigkoma (20th August 2010)

  4. #3
    Join Date
    Feb 2009
    Location
    Bydgoszcz, Poland
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operato

    Yes, it resolve this problem, but now I have another problem like this in other place :

    Aleja Code:
    1. class Aleja
    2. {
    3. public:
    4. Aleja();
    5. Aleja(QString NumerAleji);// 0-lewa,1-prawa,2-przenia
    6. QList<Strona> Strony;
    7. void DodajElement(QString NrElementu,QString NrStrony);
    8. int ZwrocLiczbeElemntowNaStronie(int NrStrony);
    9. int liczbaElementow();
    10. QString ZwrocNumerElementu(int NrStrony,int PozycjaElementu);
    11. QList<QString> nazwyStron();
    12. QString NrAleji;
    13. int lel; //liczba elementów
    14. bool CzyElementJuzIstnieje(QString NrElementu,int NrStrony);
    15. int PozycjaElementu(QString NrElementu,int NrStrony);
    16. QString LatLog;
    17. Strona pobierzStrone(QString NazwaStrony); // lewa,prawa,przednia
    18. };
    To copy to clipboard, switch view to plain text mode 

    and function where i have arror:
    Aleja::DodajElement Code:
    1. void Aleja::DodajElement(QString NrElementu,QString NrStrony){
    2. Strony.at(1).dodajElement(NrAleji,NrElementu,NrStrony);
    3. }
    To copy to clipboard, switch view to plain text mode 

    And similar error:
    Error 2 Code:
    1. passing 'const Strona' as 'this' argument of 'void Strona::dodajElement(QString, QString, QString)' discards qualifiers
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,004 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operato

    QList::at() returns a const value. It's the same as above. If you want to alter elements you have to use insert or replace, or better use the []operator.

  6. The following user says thank you to Lykurg for this useful post:

    bigkoma (20th August 2010)

  7. #5
    Join Date
    Feb 2009
    Location
    Bydgoszcz, Poland
    Posts
    16
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: passing 'const QList<Element>' as 'this' argument of 'QList<T>& QList<T>::operato

    Yes, this working and it solved my problem

    I use the []operator.

    Very, very thanks you

Similar Threads

  1. QList<MyClass> to const QList<const MyClass>
    By juanluispaz in forum Newbie
    Replies: 4
    Last Post: 6th May 2010, 19:27
  2. Copying an QList into a new, sorted QList.
    By Thomas Wrobel in forum Newbie
    Replies: 3
    Last Post: 11th January 2010, 18:27
  3. Change a QList element
    By mcrahr in forum Newbie
    Replies: 1
    Last Post: 23rd August 2009, 11:16
  4. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08:42
  5. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28

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.