使用 QApplication::applicationDirPath 调用重载'arg(QString (&)())'时,调用是不明确的,为什么?

Call of overloaded 'arg(QString (&)())' is ambiguous when called using QApplication::applicationDirPath, why?

本文关键字:调用 不明确 为什么 arg applicationDirPath QApplication 重载 使用 QString      更新时间:2023-10-16
QString msgText = QString("The file has been saved at %1sysconf.xml").arg(QApplication::applicationDirPath);

给了我上面的错误。我以前用过.arg(),所以我想知道为什么它会给我这个错误?我代码中的所有其他.arg()都可以正常工作。

解释

QApplication::applicationDirPath是一个静态成员函数,要获得你要找的值,你必须这样对待它,因此;你必须调用该函数。

目前您正在尝试将函数指针传递给QString::arg,并且由于编译器找不到适合此类构造的重载,因此会引发诊断。


解决方案

QString msgText = QString(...).arg(QApplication::applicationDirPath ());

注意:请参阅QApplication::applicationDirPath后添加的()

尝试实际调用该函数:

QString msgText = QString("The file has been saved at %1sysconf.xml").arg(QApplication::applicationDirPath());

请注意应用程序目录路径后面的"(("。

根据这一点,您将传递一个指向静态成员函数applicationDirPath的指针,并且arg有几个重载,它们都不接受这样的 poitner。您的编译器似乎发现了函数指针到参数类型的多次转换arg这些参数类型被重载并感到困惑。你的意思是:

QString msgText = QString("The file has been saved at %1sysconf.xml")
  .arg(QApplication::applicationDirPath());
                                 //    ^^--!!