Qt5:使用QCommandLineParser类时出现奇怪的编译错误
Qt5: Strange compilation error while using QCommandLineParser class
对于我的应用程序,我必须派生QtCoreApplication并使用QCommandLineParser。我在一个单独的名称空间中声明了QCommandLineOptions实例,并希望在这个名称空间中也声明解析器。然而,我犯了一个我不太理解的错误。
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);
}
在这两种情况下,我做错了什么?
命名空间声明中不能有像parser.addOption(optA)
或options << optA
这样的表达式。这只是C++的事情,与Qt无关。我建议您将parser
和optA
变量放在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);
...
}
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?