私はこの次のコードを持っています:
template <class T>
bool loadCSV (const QString &filename, map<T,int> &mapping){
QFile boxfile;
boxfile.setFileName(filename);
QString line;
QStringList list;
if (!boxfile.open(QIODevice::ReadOnly)){
cout << "Could not open box data file." << endl;
return false;
}
QTextStream stream2( &boxfile );
while (!stream2.atEnd()){
line = stream2.readLine();
list = line.split(',');
mapping[list.front().toInt()]=list.back().toInt();
}
return true;
}
CSVファイルを取得し、次の場所に貼り付けます。
map<int, int> mapping
構造。これはテンプレートとして実行されるため、データをに貼り付けるためにも使用できます。
map<string, int> mapping
構造。これを行うには、whileループの最後の行を変更する必要がありますが、これを実現するための最良の方法がわかりません。
私はいくつかの方法を考えることができました:
いくつかの方法でタイプを検出し、そこにある種の条件付き行があります(これが可能かどうか、可能であればどのように行うかは実際にはわかりません。
これを行うには、QStringListにクラス関数を追加します。
私はオプション2を試し、これを行いました:
void QStringList::cInsert(map<int,int> &mapping){
mapping[this->front().toInt()]=this->back().toInt();
}
void QStringList::cInsert(map<string,int> &mapping){
mapping[(this->front()).toAscii()]=this->back().toInt();
}
QStringListクラスでこれらの関数を定義する必要もあったため、これは機能しませんでした。そのため、これは少し面倒になります。その代わり。QStringListから継承しようとしています:
class myQStringList: public QStringList{
public:
void cInsert(map<int,int> &mapping);
void cInsert(map<string,int> &mapping);
};
void myQStringList::cInsert(map<int,int> &mapping){
mapping[this->front().toInt()]=this->back().toInt();
}
void myQStringList::cInsert(map<string,int> &mapping){
mapping[(this->front()).toAscii()]=this->back().toInt();
}
そして、コードを変更します。
template <class T>
bool loadCSV (const QString &filename, map<T,int> &mapping){
QFile boxfile;
boxfile.setFileName(filename);
QString line;
myQStringList list;
if (!boxfile.open(QIODevice::ReadOnly)){
cout << "Could not open box data file." << endl;
return false;
}
QTextStream stream2( &boxfile );
while (!stream2.atEnd()){
line = stream2.readLine();
list = line.split(',');
list.cInsert(mapping);
}
return true;}
しかし。line.split/listの割り当てに関連してエラーが発生します。
main.cpp:123: error: no match for 'operator=' in 'list = QString::split(const QChar&, QString::SplitBehavior, Qt::CaseSensitivity) const(((const QChar&)(&QChar(44))), KeepEmptyParts, CaseSensitive)'
このエラーが何であるか、また、割り当て/コピー演算子が継承されていないことに関係しているかどうかはわかりませんか?
そして、実際の新しいクラスに関連して、次のエラーが発生します。
main.cpp:104: error: no match for 'operator[]' in 'mapping[QString::toAscii() const()]'
c:/qt/mingw/bin/../lib/gcc/mingw32/3.4.2/../../../../include/c++/3.4.2/bits/stl_map.h:332: note: candidates are: _Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = std::string, _Tp = int, _Compare = std::less<std::string>, _Alloc = std::allocator<std::pair<const std::string, int> >]
main.cpp: In function `bool loadCSV(const QString&, std::map<T, int, std::less<_Key>, std::allocator<std::pair<const T,int> > >&) [with T = int]':
私はそれを単に理解していません。誰か説明してもらえますか?
また、私がこれについて行っている方法が正しいかどうかはわかりません。これに関してもアドバイスをいただければ幸いです。ありがとう。