如何使用自定义 class 的 QVector 容器?
How to use the QVector container with a custom class?
这是我为个人项目实施的自定义 class:
#include <QtCore/QCommandLineOption>
#include <QtCore/QSharedPointer>
#include <genepy/cli/CommandLineArgument.h>
#include <genepy/cli/CommandLineOption.h>
class QCommandLineParser;
namespace genepy {
class CommandLineParserBuilder;
class ConsoleApplication;
class GENEPY_EXPORT CommandLineParser {
public:
static CommandLineParserBuilder create(const ConsoleApplication& application);
void doParsing();
template <typename T>
T getArgumentValue(const QString& name) const;
bool isOptionPresent(const QString& name) const;
template <typename T>
T getOptionValue(const QString& name) const;
private:
friend class CommandLineParserBuilder;
explicit CommandLineParser(const ConsoleApplication& application);
const QSharedPointer<QCommandLineParser> parser_;
const QCommandLineOption helpOption_;
const QCommandLineOption versionOption_;
QVector<CommandLineArgument> arguments_;
QVector<CommandLineOption> options_;
};
} // namespace genepy
完整代码可见here.
当我在 Windows(MinGW、MSVC)上用 Qt 5.15 编译这段代码时,没有问题。但是,在带有 Qt 5.7 的 Debian 9 上,我收到此错误:
In file included from /home/erwan/git/genepy/include/genepy/cli/CommandLineParserBuilder.h:29:0,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:29:
/home/erwan/git/genepy/include/genepy/cli/CommandLineParser.h:94:34: error: field ‘arguments_’ has incomplete type ‘QVector<genepy::CommandLineArgument>’
QVector<CommandLineArgument> arguments_;
^~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1139:0,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qalgorithms.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstringlist.h:41,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcommandlineparser.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCommandLineParser:1,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:26:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtypeinfo.h:189:1: note: declaration of ‘class QVector<genepy::CommandLineArgument>’
Q_DECLARE_MOVABLE_CONTAINER(QVector);
^
In file included from /home/erwan/git/genepy/include/genepy/cli/CommandLineParserBuilder.h:29:0,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:29:
/home/erwan/git/genepy/include/genepy/cli/CommandLineParser.h:97:32: error: field ‘options_’ has incomplete type ‘QVector<genepy::CommandLineOption>’
QVector<CommandLineOption> options_;
^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1139:0,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qalgorithms.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstringlist.h:41,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcommandlineparser.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCommandLineParser:1,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:26:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtypeinfo.h:189:1: note: declaration of ‘class QVector<genepy::CommandLineOption>’
Q_DECLARE_MOVABLE_CONTAINER(QVector);
^
我不知道这段代码出了什么问题...有人可以帮助我吗?非常感谢您的帮助。
最后,我将 QVector<CommandLineArgument>
更改为 QVector<QSharedPointer<CommandLineArgument>>
,因为 CommandLineArgument
不是 可分配数据类型 (它缺少默认构造函数等)。现在,它起作用了。
这是我为个人项目实施的自定义 class:
#include <QtCore/QCommandLineOption>
#include <QtCore/QSharedPointer>
#include <genepy/cli/CommandLineArgument.h>
#include <genepy/cli/CommandLineOption.h>
class QCommandLineParser;
namespace genepy {
class CommandLineParserBuilder;
class ConsoleApplication;
class GENEPY_EXPORT CommandLineParser {
public:
static CommandLineParserBuilder create(const ConsoleApplication& application);
void doParsing();
template <typename T>
T getArgumentValue(const QString& name) const;
bool isOptionPresent(const QString& name) const;
template <typename T>
T getOptionValue(const QString& name) const;
private:
friend class CommandLineParserBuilder;
explicit CommandLineParser(const ConsoleApplication& application);
const QSharedPointer<QCommandLineParser> parser_;
const QCommandLineOption helpOption_;
const QCommandLineOption versionOption_;
QVector<CommandLineArgument> arguments_;
QVector<CommandLineOption> options_;
};
} // namespace genepy
完整代码可见here.
当我在 Windows(MinGW、MSVC)上用 Qt 5.15 编译这段代码时,没有问题。但是,在带有 Qt 5.7 的 Debian 9 上,我收到此错误:
In file included from /home/erwan/git/genepy/include/genepy/cli/CommandLineParserBuilder.h:29:0,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:29:
/home/erwan/git/genepy/include/genepy/cli/CommandLineParser.h:94:34: error: field ‘arguments_’ has incomplete type ‘QVector<genepy::CommandLineArgument>’
QVector<CommandLineArgument> arguments_;
^~~~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1139:0,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qalgorithms.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstringlist.h:41,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcommandlineparser.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCommandLineParser:1,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:26:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtypeinfo.h:189:1: note: declaration of ‘class QVector<genepy::CommandLineArgument>’
Q_DECLARE_MOVABLE_CONTAINER(QVector);
^
In file included from /home/erwan/git/genepy/include/genepy/cli/CommandLineParserBuilder.h:29:0,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:29:
/home/erwan/git/genepy/include/genepy/cli/CommandLineParser.h:97:32: error: field ‘options_’ has incomplete type ‘QVector<genepy::CommandLineOption>’
QVector<CommandLineOption> options_;
^~~~~~~~
In file included from /usr/include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:1139:0,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qalgorithms.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qlist.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qstringlist.h:41,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/qcommandlineparser.h:43,
from /usr/include/x86_64-linux-gnu/qt5/QtCore/QCommandLineParser:1,
from /home/erwan/git/genepy/src/cli/CommandLineParser.cpp:26:
/usr/include/x86_64-linux-gnu/qt5/QtCore/qtypeinfo.h:189:1: note: declaration of ‘class QVector<genepy::CommandLineOption>’
Q_DECLARE_MOVABLE_CONTAINER(QVector);
^
我不知道这段代码出了什么问题...有人可以帮助我吗?非常感谢您的帮助。
最后,我将 QVector<CommandLineArgument>
更改为 QVector<QSharedPointer<CommandLineArgument>>
,因为 CommandLineArgument
不是 可分配数据类型 (它缺少默认构造函数等)。现在,它起作用了。