2012-02-16 90 views
0

有其具備以下功能類:問題與qDebug和QString的const引用

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 

當我從字符串返回刪除常量&,有用。這就像< <不適用於參考。什麼是錯的,爲什麼會崩潰?

回答

3

您返回對您退出FileName()函數時銷燬的QString的引用。

+0

哦是的!謝謝 – chikuba 2012-02-16 07:51:42

0

的std ::法院不知道QString的,你需要把它轉換爲的std :: string或爲const char *

使用QString::toStdString轉換成的std :: string,如:

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

哦,對不起。我感謝qDebug處理qstrings,但不明顯引用 – chikuba 2012-02-16 06:58:28