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



  • I created a method that accepts a const QString& as argument, and I'm getting this error when trying to replace strings on it.

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

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


  • Qt Champions 2016

    hi there
    should be a
    QString & replace(const QRegularExpression &re, const QString &after)


  • Qt Champions 2016

    hi

    try to remove the const in
    Naming:.clear(QString &name)

    then it compiles for me.



  • @mrjj - Thank you, it works.



  • @mrjj - I'm getting another error now, it's saying that cannot convert const char[] to QString... and a warning:

    returning address of local variable or temporary


  • Qt Champions 2016

    @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



  • @mrjj

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

  • Qt Champions 2016

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



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


  • Qt Champions 2016

    @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


  • Lifetime Qt Champion

    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]



  • @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?


  • Qt Champions 2016

    @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.


  • Qt Champions 2016

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

  • Lifetime Qt Champion

    @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.


  • Qt Champions 2016

    @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! :]


  • Lifetime Qt Champion

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


  • Qt Champions 2016

    @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 ... :)


Log in to reply
 

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