我有以下代码
#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)
{
//...
}
问题:
我想知道为什么要考虑qdebug.h中的模板。我有一些想法,但是给出带有指向c ++标准的适当链接和正确说明的答案是很好的。
是否有解决方法,还是应该向qt提交错误报告?
最佳答案
以下(简化的代码)生成相同的错误。
#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中
关于c++ - QT5.5中用于qDebug()的新QFlags助手的问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31475641/