0

I cannot understand why std::string converted into QString while passing it to constructor. Here is small example:

  class StringHandler
 {
 private:
     QString str;
 public:
     StringHandler(QString s): str(s) {}
  };

 int main(int argc, char *argv[])
 {
    QCoreApplication a(argc, argv);

    std::string str = "string";
    QString qstr(str); // it gives error there are no constructor QString(std::string)
    StringHandler handler(QString(str));//it does not give an error. Why?

    return a.exec();
 }

EDIT:

class StringHandler
{
public:
    StringHandler(QString s): str(s) {}
    QString str;
};

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

std::string str = "string";
StringHandler handler(QString(str));//Here should be error, but there no error. Why?
qDebug()<<handler.str; //Error is here: request for member 'str' in 'handler', which is of non-class type 'StringHandler(QString)'

return a.exec();
}
emlai
  • 37,861
  • 9
  • 87
  • 140
Leo
  • 497
  • 1
  • 5
  • 12
  • 1
    add the actual error output, please. – Marcus Müller Oct 18 '15 at 10:02
  • possible duplicate of [How to change string into QString?](http://stackoverflow.com/questions/1814189/how-to-change-string-into-qstring) – emlai Oct 18 '15 at 10:03
  • @zenith The problem is not about error. I don.t understand why this line:`StringHandler handler(QString(str))` works. – Leo Oct 18 '15 at 10:05
  • 2
    And does it compile if you remove the `QString qstr(str);` line and just leave the other one? – Matti Virkkunen Oct 18 '15 at 10:06
  • @J A. Yes. compiler gives no error and no warning. And program runs without termination, when I remove `QString qstr(str)` – Leo Oct 18 '15 at 10:09
  • 1
    **Really**, we need the compiler error that you get in the non-working state. **This normally should not work**. – Marcus Müller Oct 18 '15 at 10:09
  • your EDIT shows a completely unrelated error. you should put this in a different question. – Marcus Müller Oct 18 '15 at 10:24
  • @Marcus Müller I have edited the question – Leo Oct 18 '15 at 10:24
  • I saw that, and you made it worse. see my comment above. – Marcus Müller Oct 18 '15 at 10:25
  • Still, **where is the compiler error for the first, non-edited version**? We've asked you to add that, and you refuse to, as it seems, so I'm voting to close this because *"Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a Minimal, Complete, and Verifiable example. "* – Marcus Müller Oct 18 '15 at 10:26
  • @Marcus Müller. As I sad there no error if I remove `QString qstr(str)` line. I understand error associated with that line and I did not post it here. – Leo Oct 18 '15 at 10:30

1 Answers1

3

Say hello to the most vexing parse.

StringHandler handler(QString(str)); declares a function named handler that takes a QString and returns a StringHandler. Yes. Thanks to the C++ parsing rules.

Now the error message request for member 'str' in 'handler', which is of non-class type 'StringHandler(QString)' makes sense: handler is being treated like a function of type StringHandler(QString) and you're trying to access a member named str in it, but of course functions have no members so the compilation fails.

You can fix this by using the uniform initialization syntax:

StringHandler handler{QString(str)};

The above can't be parsed as a function declaration, and so the compilation should fail for the expected reason: no matching constructor QString(std::string).

For more info, see C++'s most vexing parse again.

Community
  • 1
  • 1
emlai
  • 37,861
  • 9
  • 87
  • 140