[Moved, Solved] problems with "const"



  • @
    //in header: QString titolo, contenuto;
    void Nota::setTitolo(const QString altropezzodaaggiungereaTitolo) const
    {
    titolo = titolo.append(altropezzodaaggiungereaTitolo);
    }

    void Nota::setContenuto(QString altropezzodaaggiungereaContenuto) const
    {
    contenuto = contenuto.append(altropezzodaaggiungereaContenuto);
    }
    @
    error is:
    @
    31: error: passing 'const QString' as 'this' argument of 'QString& QString::append(const QString&)' discards qualifiers
    @

    so, i added a const in the first method, but nothing... =(



  • Well, usually setSomething methods modify the state of the object, so they can't be qualified const.

    Thus, your both setters shouldn't be const-qualified :

    @
    void Nota::setTitolo(const QString altropezzodaaggiungereaTitolo)
    {
    titolo.append(altropezzodaaggiungereaTitolo);
    }
    @



  • XD thank you. what about here
    @
    QList<Nota> listaNote;
    listaNote.append(Nota());
    if(...)
    {
    listaNote.at(i).setContenuto(parola);
    }
    else
    {
    listaNote.at(i).setTitolo(parola);
    }
    @



  • at returns a const reference, meaning you can't modify it. You should consider using [] instead.



  • geniale (italian). thank you! =)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.