1

我有以下代码

#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 提交错误报告?
4

1 回答 1

0

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

#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);

错误报告打开于: https ://bugreports.qt.io/browse/QTBUG-47375

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

于 2015-07-22T19:53:48.420 回答