C++ qDebug和QString常量引用的问题

C++ qDebug和QString常量引用的问题,c++,qt,qstring,qdebug,C++,Qt,Qstring,Qdebug,拥有一个具有以下功能的类: FileInfoWrapper(const QFileInfo &_fileInfo) : fileInfo(_fileInfo) {} const QString& FileName() const { return fileInfo.fileName(); } 但当我这么做的时候: QFileInfo info(somePath); qDebug() << info.absoluteDir(); // works FileInf

拥有一个具有以下功能的类:

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
QFileInfo信息(somePath);

qDebug()std::cout不知道QString,需要将其转换为std::string或const char*

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

std::cout << test.FileName().toStdString();

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

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

std::cout << test.FileName().toStdString();

std::cout您返回对离开FileName()函数时被销毁的QString的引用。

您返回对离开FileName()函数时被销毁的QString的引用。

哦,我很抱歉。我是qDebug,它处理Q字符串,但不处理引用。很抱歉。我使用qDebug,它处理qString,但不处理引用