c++ - Qt5 : Strange compilation error while using QCommandLineParser class

标签 c++ qt compiler-errors qt5 qcommandlineparser

对于我的应用程序,我必须派生QtCoreApplication并使用QCommandLineParser。我在单独的命名空间中声明了QCommandLineOptions实例,并希望在此 namespace 中声明解析器。但是我收到了一个我不太理解的错误。

namespace
{
    QCommandLineParser parser;
    
    const QCommandLineOption optA("optA", "defaultOptA");
    parser.addOption(optA); <-- error: unknown type name 'parser'
}

MyApp::MyApp(int argc, char *argv[])
    :QCoreApplication(argc, argv)
{
    setApplicationName("My App");
}

我也尝试过声明一个QList<QCommandLineOption>,以便可以向其中添加选项,并使用QCommandLineParser::addOptions在运行中将其添加到解析器中,但这也不起作用。
namespace
{
    QList<QCommandLineOption> options;
    
    const QCommandLineOption optA("optA", "defaultOptA");
    options << optA; <-- error: unknown type name 'options'
}

MyApp::MyApp(int argc, char *argv[])
    :QCoreApplication(argc, argv)
{
    setApplicationName("MyApp);

}
在这两种情况下我都做错了什么?

最佳答案

namespace 声明中不能包含parser.addOption(optA)options << optA之类的表达式。这只是C++,与Qt无关。我建议您宁愿将parseroptA变量放入MyApp类中,并在MyApp构造函数中对其进行初始化

class MyApp : public QCoreApplication
{
    ...

private:
    QCommandLineParser parser;
    const QCommandLineOption optA;
};

MyApp::MyApp(int argc, char *argv[])
    : QCoreApplication(argc, argv), optA("optA", "defaultOptA")
{
    parser.addOption(optA);
    
    ...
}

关于c++ - Qt5 : Strange compilation error while using QCommandLineParser class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62866487/

相关文章:

由于 Cmake 检查,Android NDK 项目需要数年时间才能完成 gradle buid

Qt IDE c/c++Debugger 模式很慢?

c++ - Qt/C++ - 从派生类调用覆盖方法

android - Android设置维度获取错误:(100, 33) String types not allowed (at 'layout_height' with value 'wrap_content' )

c++ - 我的以下代码有错误

c++ - g++ (MinGW)、C++11 和 SSE

c++ - 现代 OpenGL 中的分形树(OpenGL 3.3 及更高版本)

python - clang.cindex.Libclang错误 "Undefined symbol clang_CXXRecord_isAbstract"

c++ - Qt 5.4 中的 "No debugger set up"

qt - 链接器错误LNK2001和LNK2019