Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. [Moved, Solved] problems with "const"
QtWS25 Last Chance

[Moved, Solved] problems with "const"

Scheduled Pinned Locked Moved C++ Gurus
5 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #1

    @
    //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... =(

    1 Reply Last reply
    0
    • O Offline
      O Offline
      octal
      wrote on last edited by
      #2

      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);
      }
      @

      1 Reply Last reply
      0
      • S Offline
        S Offline
        spode
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • O Offline
          O Offline
          octal
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • S Offline
            S Offline
            spode
            wrote on last edited by
            #5

            geniale (italian). thank you! =)

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved