繁体   English   中英

错误:C2664:&#39;QXmlStreamWriter :: writeAttributes&#39;:无法从&#39;QVector转换参数1 <T> &#39;至&#39;const QXmlStreamAttributes&&#39;

[英]error: C2664: 'QXmlStreamWriter::writeAttributes' : cannot convert parameter 1 from 'QVector<T>' to 'const QXmlStreamAttributes &'

我习惯于使用QStringList() << "a" << "b"惯用法来快速构造要传递给函数的QStringList,但是当我尝试使用QXmlStreamAttributes ,它没有用。

此代码编译:

QXmlStreamAttributes attributes;
attributes << QXmlStreamAttribute("a", "b");
writer.writeAttributes(attributes);

但这失败了:

writer.writeAttributes(QXmlStreamAttributes() << QXmlStreamAttribute("a", "b"));

它因错误而失败:

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

我注意到的另一件事:

此代码编译:

QVector<QXmlStreamAttribute> v1 = (QVector<QXmlStreamAttribute>() << QXmlStreamAttribute("a", "b"));

但是,即使QXmlStreamAttributes从QVector<QXmlStreamAttribute>继承,也不会这样:

QXmlStreamAttributes v2 = (QXmlStreamAttributes() << QXmlStreamAttribute("a", "b"));

它失败,并显示相同的错误。

知道为什么会这样吗?

QStringList

operator<<(const QString & str)

但是QVector

QVector<T> &    operator<<(const T & value)

所以你

QVector<QXmlStreamAttribute> v1 = (QVector<QXmlStreamAttribute>() << QXmlStreamAttribute("a", "b"));

编译成功。

但是您的错误是QXmlStreamAttributes没有副本构造函数,但是您尝试使用它,因此有两种解决方案:

使用append

QXmlStreamAttributes v2;
v2.append(QXmlStreamAttribute("a", "b"));
qDebug()<< v2.first().name();

或以不同的方式使用<<

QXmlStreamAttributes v2;
v2 << QXmlStreamAttribute("a", "b");
qDebug()<< v2.first().name();

在两种情况下,输出均为"a"

QXmlStreamAttributes QStringList QVector

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM