具有具有以下功能的课程:

FileInfoWrapper(const QFileInfo &_fileInfo) : fileInfo(_fileInfo) {}

const QString& FileName() const { return fileInfo.fileName(); }

但是当我这样做时:

QFileInfo info(somePath);

qDebug() << info.absoluteDir(); // works

FileInfoWrapper test(info);

qDebug() << test.FileName(); // this crashes the entire application

当我从字符串返回中删除const&时,它可以正常工作。就像<<不适合参考。怎么了,为什么会崩溃?

有帮助吗?

解决方案

您返回对QSTRING的引用,该QString在您离开filename()函数时被破坏。

其他提示

std :: cout不知道qString,您需要将其转换为std :: string或const char*

利用 QString::toStdString 转换为std :: string,例如:

std::cout << test.FileName().toStdString();
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top