使用运算符<<带有QStringList指针 [英] Using the operator<< with a QStringList pointer

查看:779
本文介绍了使用运算符<<带有QStringList指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改此代码?

  QString s =123 
QStringList * myList = new QStringList;

myList<< s;

错误:


不匹配'operator< <'(操作数类型为'QStringList *'和
'QString')




  * myList << s; 

不起作用:myList is Empty,after this。


< QStringList ,因为这是一个隐式共享类,因为这是一个隐式共享类。到写时复制。您可以在下面找到更多详细信息:



http://qt-project.org/doc/qt-5.1/qtcore/implicit-sharing.html



这意味着,我将重构您的代码,如下所示:

  QString s =123 
QStringList myList;

myList<< s;

注意,你也有一个拼写错误 myList 因为你似乎写了 myLis 。这是至少一个语法错误,将导致编译错误。如果你支持C ++ 11语法,你也可以使用C ++ 11语法:

  QString s =123 
QStringList myList({s});

这将会派上用场,当你有更多的元素插入没有连续附加行单独。 p>

但是,如果您仍然对某些原因感兴趣,您应该考虑:

  myList-> append(s); 

或作为最后手段,即使您的行应该工作,如果你没有做任何其他错误。这应该是整个代码,看看你是否做过任何其他错误:



main.cpp



  #include< QStringList> 
#include< QDebug>

int main()
{
QString s =123;
QStringList * myList = new QStringList;
* myList<< s;
qDebug()<< * myList;
return 0;
}



建筑(类似的东西)



  g ++ -Wall -fPIC -I / usr / include / qt -I / usr / include / qt / QtCore -lQt5Core main.cpp&& ./a.out 



输出



 (123)


How I can change this code?

QString s="123";
QStringList *myList=new QStringList;

myList<<s;

Error:

no match for 'operator<<' (operand types are 'QStringList*' and 'QString')

*myList<<s;

does not work too: myList is Empty, after this.

解决方案

There is little to no point in using a pointer for a QStringList because this is an implicitly shared class due to the copy-on-write. You can find further details for that below:

http://qt-project.org/doc/qt-5.1/qtcore/implicit-sharing.html

Which means, I would refactor your code to look like this:

QString s="123";
QStringList myList;

myList << s;

Note, you also had a typo for myList as you seem to have written myLis. That is at least one syntax error which would lead to compilation error. You could also use the C++11 syntax for this if you have support for that:

QString s="123";
QStringList myList({s});

This will come to handy when you will have more elements to insert without continuous append lines separately.

However, if you are still interested in doing this for some reason, you should consider this:

myList->append(s);

or as a last resort even your line should work if you had not done any other mistakes. This should be the whole code to see if you had done any other mistakes:

main.cpp

#include <QStringList>
#include <QDebug>

int main()
{
    QString s="123";
    QStringList *myList = new QStringList;
    *myList<<s;
    qDebug() << *myList;
    return 0;
}

Building (something similar)

g++ -Wall -fPIC -I/usr/include/qt -I/usr/include/qt/QtCore -lQt5Core main.cpp && ./a.out

Output

("123")

这篇关于使用运算符&lt;&lt;带有QStringList指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆