Comparison between distinct pointer types 'QString*' and 'const char*' lacks a cast [-fpermissive]
-
I had a QString inizialized in the .h file
@
QString *str;
@In the .cpp constructor, str id declared
@
active = new QString;
if (str->isEmpty()) { *str = "foobar"; }
@Now, when I try to set an int variable in this way
@
int num = (str == "foobar") ? 5: 6;
@Qt Creator sends me this error:
"comparison between distinct pointer types 'QString*' and 'const char*' lacks a cast [-fpermissive]"
I guess that I have not run a cast, but what exactly?
Could you help me? -
Hi,
You should dereference str using * operator during comparison.
This will work,
@
int num = (*str == "foobar") ? 5: 6;
@ -
In your first post i see you have initialised active but not str. Is that a typo ?
-
So is it still crashing after initialising str ? I mean after doing this,
@
QString *str = new QString;
if (str->isEmpty()) { *str = "foobar"; }
int num = (*str == "foobar") ? 5: 6;
@ -
Good. You can mark the post as solved.
-
Hi,
Out of curiosity, why are you using a pointer to a QString ?