我正在检查提供的路径是否存在,以及它是否是一个文件。
所以我写了一段代码:
#include <QFile>
#include <QFileInfo>
bool Tool::checkPath(const QString &path){
QFileInfo fileInfo(QFile(path));
return (fileInfo.exists() && fileInfo.isFile());
}我得到以下编译器错误:
Error: request for member 'exists' in 'fileInfo', which is of non-class type 'QFileInfo(QFile)'
Error: request for member 'isFile' in 'fileInfo', which is of non-class type 'QFileInfo(QFile)'
为什么?我一遍又一遍地读着文件,但我弄不懂。顺便说一下,Qt建议我使用这些方法并完成它们。但是编译器不喜欢它。
发布于 2014-05-29 10:32:38
似乎是vexing parse:编译器认为
QFileInfo fileInfo(QFile(path));是一个类似于QFileInfo fileInfo(QFile path);的函数定义。
代之以使用类似于:
QFile ff(file);
QFileInfo fileInfo(ff);
bool test = (fileInfo.exists() && fileInfo.isFile());发布于 2017-06-23 09:37:23
您可以直接使用
QFileInfo fileInfo(path)
bool test = fileInfo.exists() && fileInfo.isFile()发布于 2017-12-01 12:11:11
由于我的名声很低,我不能发表评论,所以我在这里放了一些关于QFileInfo的台词:
QFileInfo fileInfo("./.py");
bool test = fileInfo.exists() && fileInfo.isFile();那个例子会失败的!
我在检查中添加了baseName()值:
bool test = fileInfo.exists() && fileInfo.isFile() && !fileInfo.baseName().isEmpty();https://stackoverflow.com/questions/23930754
复制相似问题