【发布时间】:2016-01-16 17:59:55
【问题描述】:
我不明白为什么std::string 在将其传递给构造函数时会转换为 QString。这是一个小例子:
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();
}
编辑:
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();
}
【问题讨论】:
-
请添加实际的错误输出。
-
@zenith 问题不在于错误。我不明白为什么这条线:
StringHandler handler(QString(str))有效。 -
如果您删除
QString qstr(str);行并保留另一行,它是否可以编译? -
真的,我们需要你在非工作状态下得到的编译器错误。 这通常不应该工作。
标签: c++ constructor compiler-errors most-vexing-parse