[英]Qt5: Strange compilation error while using QCommandLineParser class
For my application I had to derive QtCoreApplication and use QCommandLineParser.对于我的应用程序,我必须派生 QtCoreApplication 并使用 QCommandLineParser。 I declared QCommandLineOptions instances in a separate namespace and wanted to declare the parser in this namepsace as well.我在一个单独的命名空间中声明了 QCommandLineOptions 实例,并且还想在这个命名空间中声明解析器。 However I get an error that I don't quite understand.但是我得到一个我不太明白的错误。
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");
}
I have also tried declaring a QList<QCommandLineOption>
so that I can add the options to it and add it the parser in on go using QCommandLineParser::addOptions
, but that does not work either.我也尝试过声明一个QList<QCommandLineOption>
以便我可以添加选项并使用QCommandLineParser::addOptions
在 go 上添加解析器,但这也不起作用。
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);
}
What am I doing wrong in both cases?在这两种情况下我做错了什么?
You can't have expressions like parser.addOption(optA)
or options << optA
in a namespace declaration.在命名空间声明中不能有像parser.addOption(optA)
或options << optA
这样的表达式。 This is just C++ thing and has nothing to do with Qt.这只是 C++ 的事情,与 ZE8801102A40AD89DDCFDCAEBF008D25Z 无关。 I would suggest you rather put the parser
and optA
variables in your MyApp
class and initialize them in the MyApp
constructor我建议您宁愿将parser
和optA
变量放在MyApp
class 中,并在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);
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.