2010-12-14 138 views
2

我试图同时打开多个文件(随机文件数),并使用其他代码存储在自己的QList textstreams简单:QT:商店QTextStream中的QList

QList<QTextStream> files; 
QList<QString> fnames; 
fnames.append("file1.txt"); 
fnames.append("file2.txt"); 
// ..and so on with random iterations 

// collect qtextsrams into qlist 
foreach (QString file, fnames) { 
     QFile f(file); 
     f.open(QIODevice::ReadOnly); 
     QTextStream textStream(&f); 
     files2.append(&textStream); 
} 

// use qtextstreams in a loop 
QList<QTextStream>::iterator i; 
for (i = files.begin(); i != files.end(); ++i) { 
     qDebug() << i->readLine(); 
} 

所以我有一个错误:

/make debug 
make -f Makefile.Debug 
make[1]: Entering directory `/home/pixx/Workspace/collocs' 
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -Idebug -o debug/main.o main.cpp 
main.cpp: In function ‘int main(int, char**)’: 
main.cpp:128: error: no matching function for call to ‘QList<QTextStream>::append(QTextStream*)’ 
/usr/include/qt4/QtCore/qlist.h:493: note: candidates are: void QList<T>::append(const T&) [with T = QTextStream] 
/usr/include/qt4/QtCore/qlist.h:819: note:     void QList<T>::append(const QList<T>&) [with T = QTextStream] 
main.cpp:117: warning: unused variable ‘cc’ 
In file included from /usr/include/qt4/QtCore/QList:1, 
       from main.cpp:1: 
/usr/include/qt4/QtCore/qtextstream.h: In member function ‘void QList<T>::node_copy(QList<T>::Node*, QList<T>::Node*, QList<T>::Node*) [with T = QTextStream]’: 
/usr/include/qt4/QtCore/qlist.h:695: instantiated from ‘void QList<T>::detach_helper(int) [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:709: instantiated from ‘void QList<T>::detach_helper() [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:126: instantiated from ‘void QList<T>::detach() [with T = QTextStream]’ 
/usr/include/qt4/QtCore/qlist.h:254: instantiated from ‘QList<T>::iterator QList<T>::begin() [with T = QTextStream]’ 
main.cpp:133: instantiated from here 
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private 
/usr/include/qt4/QtCore/qlist.h:386: error: within this context 
/usr/include/qt4/QtCore/qtextstream.h:258: error: ‘QTextStream::QTextStream(const QTextStream&)’ is private 
/usr/include/qt4/QtCore/qlist.h:399: error: within this context 
make[1]: Leaving directory `/home/pixx/Workspace/collocs' 
make[1]: *** [debug/main.o] Error 1 
make: *** [debug] Error 2 

我该怎么办? 我明白这是非常简单的问题,但我找不到正确的查询谷歌:(

回答

4

第一个错误,即“没有匹配函数调用'QList :: append(QTextStream *)'”是造成通过你在这行中使用&操作:

files2.append(&textStream); 

您的列表应该进行QTextStream对象,不是指针到QTextStream对象

但真正的问题在于更深将物体放入一个。列表中,一个对象必须有复制构造函数。QTextStream没有,因为它不清楚差异同一文本流的不同副本应该一起工作。我建议你创建一个指向文本流的指针列表,如“QList”中所示。当然,在这种情况下,不要忘记处理他们的毁灭,当他们不再需要:

foreach (QTextStream *cur, files) delete cur; 

如果你需要通过你的代码的不同部分之间的这份名单中,使之与这样的多个副本,您可能需要智能指针(QSharedPointer),但我很难想到您需要将它用于文本流的任务。

+0

也许有一个更简单的解决方案,我的最初问题?我需要打开随机数量的文件,然后从其中一些文件中读取单行而不重新打开。 – pixx 2010-12-14 10:50:21

+0

我需要这个来实现合并有序文件的算法为: // - 打开N个结果文件 // - 读取每个文件的一行 // - 直到两个文件完全读取: // - 是单词相同? // - 是 - >将单词和总和写入输出文件 //读取两个文件中的下一行 // - no - >只将字母表中最低的单词写入输出文件 //读取从这个单词的结果文件的下一行 – pixx 2010-12-14 10:55:26

2

我找到了一个解决方案,感谢Septagram的想法! QList文件2; //文件列表合并

QList<QTextStream> files; 
QList<QString> fnames; 
fnames.append("file1.txt"); 
fnames.append("file2.txt"); 
// ..and so on with random iterations 
QList<QFile *> files2; // file list to merge 
QList<QTextStream *> files3; 
foreach (QString file, files) { 
    files2.append(new QFile(file)); // create file obj 
    files2.last()->open(QIODevice::ReadOnly); // open file 
    files3.append(new QTextStream(files2.last())); // create textstream 
} 

QList< QTextStream * >::iterator i3; 
for (i3 = files3.begin(); i3 != files3.end(); ++i3) { 
     qDebug() << (*i3)->readLine(); 
}