繁体   English   中英

如何将 QVector 容器与自定义 class 一起使用?

[英]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

完整的代码可以在这里找到。

当我在 Windows(MinGW,MSVC)上使用 Qt 5.15 编译此代码时,没有问题。 但是,在 Debian 9 和 Qt 5.7 上,我收到此错误:

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不是可分配的数据类型(它缺少默认构造函数等)。 现在,它起作用了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM