【问题标题】:Qt 5.7.1 / gcc 6.3.0: error: body of constexpr function 'static constexpr int QMetaTypeId2<T>Qt 5.7.1 / gcc 6.3.0:错误:constexpr 函数体'static constexpr int QMetaTypeId2<T>
【发布时间】:2020-12-11 03:05:18
【问题描述】:

这段代码

namespace Config {
  class Value {
  public:
    enum Type {
      Null,
      Integer,
      String,
      Map,
      List,
      Boolean
    };

    Value();
    Value(int v);
    Value(const QString &v);
    Value(const QMap<QString, Value> &v);
    Value(const QList<Value> &v);
    Value(bool v);

    template<class T> T get() const {
      return value.value<T>();
    }

    enum Type type() const;
    enum Type listType() const;
    bool isNull() const;

    void setListType(enum Type t);

    operator QString() const;
    QString toString() const;

  private:
    QVariant value;
    enum Type value_type;
    enum Type list_elements_type;
  };
}

Q_DECLARE_METATYPE(Config::Value)

(https://github.com/olegantonyan/mpz/blob/master/app/config/value.h) 在最新版本的 gcc/qt 上编译,但在 Debian 伸展(gcc 6.3.0,qt 5.7.1)上失败

../../../../include/x86_64-linux-gnu/qt5/QtCore/qglobal.h:746:47: error: static assertion failed: Type is not registered, please use the Q_DECLARE_METATYPE macro to make it known to Qt's meta-object system

../../../../include/x86_64-linux-gnu/qt5/QtCore/qmetatype.h:1604:100: error: body of constexpr function 'static constexpr int QMetaTypeId2<T>::qt_metatype_id() [with T = std::nullptr_t]' not a return-statement

完整的构建日志https://build.opensuse.org/package/live_build_log/home:oleg_antonyan/mpz/Debian_9.0/x86_64

在 CentOS7 (gcc 4.8.5, qt 5.6.1) 上也可以正常工作。尚未在更古老的版本上进行测试。

你有什么想法吗?

【问题讨论】:

  • 消息"...body of constexpr function ... not a return-statement" 表明您正在编译为c++11,其中c++14 或更高版本(我认为?)是必需的。
  • c++14 没有帮助
  • 由于它报告“类型未注册”,您是否尝试添加Q_DECLARE_METATYPE(Config::Value::Type)
  • 另外,您是否尝试将Q_DECLARE_METATYPE 移动到源文件而不是头文件?我注意到(在我的代码中)它总是从 cpp 文件调用,而不是从 h 文件调用。
  • Q_DECLARE_METATYPE(Config::Value::Type) 没有改变任何东西 将 Q_DECLARE_METATYPE 移动到 cpp 文件也不起作用,但在所有系统上(不仅是 debian 9)

标签: c++ qt c++11 qt5 debian-stretch


【解决方案1】:

发现问题: value.setValue(nullptr); 在构造函数中,其中 value 是 QVariant。

删除了这一行,现在它可以编译了。不知道为什么我首先把它放在那里

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2020-02-19
    相关资源
    最近更新 更多