Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QString::replace 13 overloads have no legal conversion for 'this' pointer
Forum Updated to NodeBB v4.3 + New Features

QString::replace 13 overloads have no legal conversion for 'this' pointer

Scheduled Pinned Locked Moved Unsolved General and Desktop
18 Posts 4 Posters 6.0k Views 3 Watching
  • 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.
  • mrjjM mrjj

    @Volebab
    u need to show the actual line where it says so :)

    also you make return "QString &" meaning a reference and then you give it
    " return result;"
    which is local variable so I assume it was its mad about

    so return copy
    QString Naming(..)

    if its that line it complains about

    VolebabV Offline
    VolebabV Offline
    Volebab
    wrote on last edited by
    #7

    @mrjj

    Naming naming;
    auto result = naming.clear("tretacor.hs");
    
    mrjjM 1 Reply Last reply
    0
    • VolebabV Volebab

      @mrjj

      Naming naming;
      auto result = naming.clear("tretacor.hs");
      
      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #8

      @Volebab

      Well you give it a char *, not a QString
      Naming naming;
      auto result = naming.clear("tretacor.hs");
      when it wants QString

      Naming naming;
      auto result = naming.clear(QString("tretacor.hs"));

      VolebabV 1 Reply Last reply
      0
      • mrjjM mrjj

        @Volebab

        Well you give it a char *, not a QString
        Naming naming;
        auto result = naming.clear("tretacor.hs");
        when it wants QString

        Naming naming;
        auto result = naming.clear(QString("tretacor.hs"));

        VolebabV Offline
        VolebabV Offline
        Volebab
        wrote on last edited by
        #9

        @mrjj But I thought that it would convert like std::string does.

        mrjjM kshegunovK 2 Replies Last reply
        0
        • VolebabV Volebab

          @mrjj But I thought that it would convert like std::string does.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #10

          @Volebab
          well you say & and then it wont, it seems

          this does work
          void TakeIt(QString test) {}
          TakeIt("dddd");

          So it seems that const char wont convert to QString &
          directly

          1 Reply Last reply
          1
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by SGaist
            #11

            Hi,

            Like @mrjj wrote, it complains that you are trying to return a reference to a variable that exists only during the lifetime of the function.

            Either pass your QString as reference to your function or return a copy.

            So either:

            QString Naming::clear(QString &name)
            {
                return name.replace(
                    QRegularExpression("hs|ts"), "hss"
                );
            }
            

            or

            QString & Naming::clear(QString &name)
            {
                return name.replace(
                    QRegularExpression("hs|ts"), "hss"
                );
            }
            

            [edit: Fixed code sample]

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            VolebabV 1 Reply Last reply
            1
            • SGaistS SGaist

              Hi,

              Like @mrjj wrote, it complains that you are trying to return a reference to a variable that exists only during the lifetime of the function.

              Either pass your QString as reference to your function or return a copy.

              So either:

              QString Naming::clear(QString &name)
              {
                  return name.replace(
                      QRegularExpression("hs|ts"), "hss"
                  );
              }
              

              or

              QString & Naming::clear(QString &name)
              {
                  return name.replace(
                      QRegularExpression("hs|ts"), "hss"
                  );
              }
              

              [edit: Fixed code sample]

              VolebabV Offline
              VolebabV Offline
              Volebab
              wrote on last edited by
              #12

              @SGaist said:

              QString Naming::clear(const QString &name)
              {
              return name.replace(
              QRegularExpression("hs|ts"), "hss"
              );
              }

              I tried and it didn't work, the same error. I think that we are not supposed to change const QString, am I right?

              kshegunovK 1 Reply Last reply
              0
              • VolebabV Volebab

                @mrjj But I thought that it would convert like std::string does.

                kshegunovK Offline
                kshegunovK Offline
                kshegunov
                Moderators
                wrote on last edited by
                #13

                @Volebab

                But I thought that it would convert like std::string does.

                The thing is, there's nothing to be converted. You're requesting that an address to an object (what a reference is) be returned, but that address is valid only inside the current stack frame, so the compiler is warning you (or giving an error depending on the actual compiler) that you can do it, but it's not a good idea - the object will be freed when the stack is unwinding, and the returned address will point to a place that doesn't in fact hold any object. It's the same as the following:

                const char * myFunction()
                {
                    char someString[5];
                    return someString;  //< This is possible in principle, but since the data behind someString is freed when the function goes out of scope, you'd get a dangling pointer.
                }
                

                When returning a variable from a function in most cases you have to return by value (as @mrjj pointed out), however don't worry about data copying, as Qt's QString is implicitly shared and the actual string won't be copied, only the pointer that QString holds to that data.

                @SGaist
                With the provided definition of Naming::clear this snippet:

                QString & Naming::clear(QString &name)
                {
                    return name.replace(
                        QRegularExpression("hs|ts"), "hss"
                    );
                }
                

                doesn't seem quite right.

                Read and abide by the Qt Code of Conduct

                1 Reply Last reply
                1
                • VolebabV Volebab

                  @SGaist said:

                  QString Naming::clear(const QString &name)
                  {
                  return name.replace(
                  QRegularExpression("hs|ts"), "hss"
                  );
                  }

                  I tried and it didn't work, the same error. I think that we are not supposed to change const QString, am I right?

                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #14

                  @Volebab

                  I tried and it didn't work, the same error. I think that we are not supposed to change const QString, am I right?

                  Yes, there is no immutable (const method) overload for QString::replace, so you have to use a non-const object.

                  This should suffice:

                  QString Naming::clear(QString name)
                  {
                      return name.replace(QRegularExpression("hs|ts"), "hss");
                  }
                  

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #15

                    @kshegunov basically you return the reference you gave as input. Not very useful I agree. I'd just made it a void function.

                    @Volebab Indeed, I've mixed replace with another function, sorry.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    kshegunovK 1 Reply Last reply
                    0
                    • SGaistS SGaist

                      @kshegunov basically you return the reference you gave as input. Not very useful I agree. I'd just made it a void function.

                      @Volebab Indeed, I've mixed replace with another function, sorry.

                      kshegunovK Offline
                      kshegunovK Offline
                      kshegunov
                      Moderators
                      wrote on last edited by
                      #16

                      @SGaist said:

                      basically you return the reference you gave as input. Not very useful I agree. I'd just made it a void function.

                      I hadn't checked the documentation before I wrote the comment, so indeed, it should work normally. I incorrectly expected QString::replace to return a copy (with replacements done), hence the confusion. Sorry! :]

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #17

                        @kshegunov No worries, it also happened to me several times ;)

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        kshegunovK 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          @kshegunov No worries, it also happened to me several times ;)

                          kshegunovK Offline
                          kshegunovK Offline
                          kshegunov
                          Moderators
                          wrote on last edited by
                          #18

                          @SGaist
                          auto always confuses me, as there's no way to tell what the heck is the type of the variable without knowing the code (or the docs) by heart ... but I suppose I'm simply old-fashioned ... :)

                          Read and abide by the Qt Code of Conduct

                          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