Unsolved 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
-
hi there
should be a
QString & replace(const QRegularExpression &re, const QString &after) -
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[]
toQString
... and a warning:returning address of local variable or temporary
-
@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 aboutso return copy
QString Naming(..)if its that line it complains about
-
Naming naming; auto result = naming.clear("tretacor.hs");
-
Well you give it a char *, not a QString
Naming naming;
auto result = naming.clear("tretacor.hs");
when it wants QStringNaming naming;
auto result = naming.clear(QString("tretacor.hs")); -
@mrjj But I thought that it would convert like std::string does.
-
@Volebab
well you say & and then it wont, it seemsthis does work
void TakeIt(QString test) {}
TakeIt("dddd");So it seems that const char wont convert to QString &
directly -
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?
-
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 thatQString
holds to that data.@SGaist
With the provided definition ofNaming::clear
this snippet:QString & Naming::clear(QString &name) { return name.replace( QRegularExpression("hs|ts"), "hss" ); }
doesn't seem quite right.
-
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"); }
-
@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.
-
@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! :] -
@kshegunov No worries, it also happened to me several times ;)
-
@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 ... :)