[英]error: C2664: 'QXmlStreamWriter::writeAttributes' : cannot convert parameter 1 from 'QVector<T>' to 'const QXmlStreamAttributes &'
I'm used to using the QStringList() << "a" << "b"
idiom for quickly constructing a QStringList to pass to a function, but when I tried it with QXmlStreamAttributes
, it didn't work. 我习惯于使用
QStringList() << "a" << "b"
惯用法来快速构造要传递给函数的QStringList,但是当我尝试使用QXmlStreamAttributes
,它没有用。
This code compiles: 此代码编译:
QXmlStreamAttributes attributes;
attributes << QXmlStreamAttribute("a", "b");
writer.writeAttributes(attributes);
But this one fails: 但这失败了:
writer.writeAttributes(QXmlStreamAttributes() << QXmlStreamAttribute("a", "b"));
It fails with error: 它因错误而失败:
C:\Workspace\untitled5\mainwindow.cpp:18: error: C2664: 'QXmlStreamWriter::writeAttributes' : cannot convert parameter 1 from 'QVector<T>' to 'const QXmlStreamAttributes &'
with
[
T=QXmlStreamAttribute
]
Reason: cannot convert from 'QVector<T>' to 'const QXmlStreamAttributes'
with
[
T=QXmlStreamAttribute
]
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
This code compiles: 此代码编译:
QVector<QXmlStreamAttribute> v1 = (QVector<QXmlStreamAttribute>() << QXmlStreamAttribute("a", "b"));
But this one doesn't, even though QXmlStreamAttributes inherits from QVector<QXmlStreamAttribute>
: 但是,即使QXmlStreamAttributes从
QVector<QXmlStreamAttribute>
继承,也不会这样:
QXmlStreamAttributes v2 = (QXmlStreamAttributes() << QXmlStreamAttribute("a", "b"));
It fails with the same error. 它失败,并显示相同的错误。
Any idea why this happens? 知道为什么会这样吗?
QStringList
has QStringList
有
operator<<(const QString & str)
but QVector
has 但是
QVector
有
QVector<T> & operator<<(const T & value)
so your 所以你
QVector<QXmlStreamAttribute> v1 = (QVector<QXmlStreamAttribute>() << QXmlStreamAttribute("a", "b"));
compiles successfully. 编译成功。
But your mistake is that QXmlStreamAttributes
has no copy constructor, but you try use it, so you have 2 solutions: 但是您的错误是
QXmlStreamAttributes
没有副本构造函数,但是您尝试使用它,因此有两种解决方案:
Use append
: 使用
append
:
QXmlStreamAttributes v2;
v2.append(QXmlStreamAttribute("a", "b"));
qDebug()<< v2.first().name();
Or use <<
in some different way: 或以不同的方式使用
<<
:
QXmlStreamAttributes v2;
v2 << QXmlStreamAttribute("a", "b");
qDebug()<< v2.first().name();
Output is "a"
in both cases. 在两种情况下,输出均为
"a"
。
QXmlStreamAttributes
QStringList
QVector
QXmlStreamAttributes
QStringList
QVector
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.