【问题标题】:Problems with new QFlags helpers for qDebug() in QT5.5QT5.5 中 qDebug() 的新 QFlags 助手的问题
【发布时间】:2015-07-17 12:14:58
【问题描述】:

我有以下代码

#include <QDebug>  //Needed for other stuff!!
#include <QDataStream>

class TestClass {
public:
    class SubClass {
    public:
        QString a;
        int b;

        /*
        //Needed for QDataStream but not relevant for the generated error
        friend QDataStream& operator<<(QDataStream& os, SubClass const& f)
        {
            os << f.a;
            os << f.b;
            return os;
        }

        friend QDataStream& operator>>(QDataStream& is, SubClass& f)
        {
            is >> f.a;
            is >> f.b;
            return is;
        }*/

        friend QDataStream& operator<< <SubClass>(QDataStream&, QList<SubClass> const&);
        friend QDataStream& operator>> <SubClass>(QDataStream&, QList<SubClass>&);

    };
};

使用 qt5.5 编译会出现以下错误:

/usr/include/qt/QtCore/qflags.h:88: error: invalid application of 'sizeof' to an incomplete type 'TestClass::SubClass'
    Q_STATIC_ASSERT_X((sizeof(Enum) <= sizeof(int)),
                   ^~~~~~~~~~~~
/usr/include/qt/QtCore/qtypetraits.h:478: error: 'TestClass::SubClass' is an incomplete type
    : integral_constant<bool, (T(0) < T(-1))> {};
                               ^
/usr/include/qt/QtCore/qdebug.h:279: in instantiation of template class 'QtPrivate::IsQEnumHelper<QFlags<TestClass::SubClass> >' requested here
    QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
                                                     ^
main.cpp:25: while substituting explicitly-specified template arguments into function template 'operator<<'
        friend QDataStream& operator<< <SubClass>(QDataStream&, QList<SubClass> const&);
                            ^

原因似乎在于QDebug头文件(qdebug.h:277)的如下代码:

template <class T>
inline typename QtPrivate::QEnableIf<
    QtPrivate::IsQEnumHelper<T>::Value || QtPrivate::IsQEnumHelper<QFlags<T> >::Value,
    QDebug>::Type
operator<<(QDebug debug, const QFlags<T> &flags)
{
  //...
}

使用 qt5.4 编译可以正常工作。取了正确的模板函数(qdatastream.h:241)

template <typename T>
QDataStream& operator<<(QDataStream& s, const QList<T>& l)
{
   //...
}

问题:

  1. 我想知道为什么要考虑 qdebug.h 中的模板。我有一些想法,但如果能提供与 c++ 标准的适当链接和适当解释的答案会很好。
  2. 是否有解决方法或者我应该向 qt 提交错误报告?

【问题讨论】:

    标签: c++ qt qt5.5


    【解决方案1】:

    以下(简化代码)产生相同的错误。

    #include <QDebug>  //Needed to cause the error
    
    class TestClassA {};
    class TestClassB {};
    
    template <typename T>
    TestClassA& operator<<(TestClassA& s, T& l) {
        Q_UNUSED(l)
        return s;
    }
    
    template TestClassA& operator<< <TestClassB>(TestClassA& s, TestClassB& l);
    

    Bugreport 打开于:https://bugreports.qt.io/browse/QTBUG-47375

    更新: 他们修好了。该修复将包含在 Qt 5.5.1 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 2011-01-07
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多