2010-12-14 1 views
2
に保管QTextStream

私は同時に複数のファイルを開くには、(ファイルの乱数を)しようとし、他のコードで使用して単純なためQListの中で自分のtextstreamsを格納しています:QT: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 

修正する必要はありますか? 私はそれは非常に単純な質問だが、私はGoogleのための正しいクエリを見つけることができません:(

答えて

4

"QList :: append(QTextStream *) 'への呼び出しのための一致機能がない" 。あなたのリストは

QTextStreamのオブジェクトではなく、オブジェクトをQTextStreamへのポインタで作られることになっしかし、本当の問題は、より深い位置にオブジェクトを置くことです

files2.append(&textStream); 

:あなたはこの行の&演算子を使用して、リストには、オブジェクトがコピーコンストラクタを持っている必要があります。QTextStreamにはどのような違いがありますか?同じテキストストリームの異なるコピーが一緒に機能するはずです。 "QList"のように、テキストストリームへのポインタのリストを作成することをお勧めします。もちろん、その場合には、それらが不要になっている場合、その破壊を処理しないことを忘れないでください:

foreach (QTextStream *cur, files) delete cur; 

あなたのコードの異なる部分の間、このリストを渡す必要がある場合は、それの複数のコピーを作成して、そのようなスマートポインタ(QSharedPointer)が必要な場合がありますが、テキストストリームに対して行う必要がある作業はほとんどありません。

+0

私の最初の問題の解決法はもっと簡単でしょうか?私はファイルのランダムな番号を開く必要があり、その後、再オープンせずにそれらのいくつかから単一の行を読み取ります。 – pixx

+0

マージされたファイルのアルゴリズムを1つに実装する必要があります: // N個の結果ファイルを開きます //各ファイルの行を読み込みます // - 両方のファイルが完全に読み取られるまで: // - は単語です同一ですか? // - はい - >出力ファイルに単語とその合計を書き込みます //両方のファイルの次の行を読み込みます //アルファベットの中で最も小さい単語のみを出力ファイルに書き込みます //読み取りこの単語の結果ファイルの次の行 – pixx

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(); 
} 
関連する問題