2012-01-01 8 views
0

私が作成していイテレータ:奇妙な行動やSTD ::マップ:: const_iteratorの

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin(); 
      typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end(); 

私のレポートクラスが見えます:cppの中

class Report 
{ 
public: 
typedef std::map<boost::filesystem3::path, 
       std::pair<unsigned long long/*code*/, 
          unsigned long long/*comment*/> > container_type_for_processed_files; 
container_type_for_processed_files processed_files()const; 
private: 
container_type_for_processed_files processed_files_; 
}; 

処理されたファイルは、次のようになります。

typename Report::container_type_for_processed_files Report::processed_files()const 
{ 
    return processed_files_; 
} 

ただし、最初の行に示すようにイテレータを初期化した後:

typename Report::container_type_for_processed_files::const_iterator beg = rep.processed_files().begin(); 
      typename Report::container_type_for_processed_files::const_iterator end = rep.processed_files().end(); 
      while (beg != end) 
      { 
       qDebug() << beg->first.c_str();//here I'm getting runtime error 
       fout << "File name: " << (beg->first).c_str(); 


       ++beg; 
      } 

エラーが発生しました:無効なパラメータがCランタイム関数に渡されました。イテレータを初期化しようとすると
私はまた、出力ペインにメッセージを取得しています:
(内部エラー:なくSYMTABで、psymtabに読んでPCの0x201です。)
何が起こっているの?

+2

'qDebug'は何ですか? –

+2

また、最小限のテストケースを作成することを検討してください(http://sscce.orgを参照)。 –

+2

@OliCharlesworthデバッグの目的でQtによって提供される出力ストリーム。 –

答えて

3

コンパイルサンプルがなければ、私はこれがあなたの問題でわからないんだけど、これは魚になります

container_type_for_processed_files processed_files() const; 

あなたが最も可能性が高いそうでない場合は、その関数が返すされ、そこにコンテナにconst&を返すべきです(潜在的に一時的な)コピーは無効になります(同じオブジェクトに対するイテレータではなく、ライフタイムが終了した一時オブジェクトへのイテレータも可能です)。

で試してみてください:

container_type_for_processed_files const& processed_files() const; 
+1

ありがとう、+1、それはよく見つかった – smallB

+0

@smallB: '+ 1'を与えるのを忘れた:D。 – Nawaz

0

1つの問題は、rep.processed_files()が、元のコンテナのをコピー戻すことです。したがって、begendは2つの別々のオブジェクトに適用されます。あるマップの始めから別のマップの終わりまで反復しようとするのは意味がありません。最初のコンテナの終わりを反復します。含まれているオブジェクトへの参照を返す必要があります。

typename Report::container_type_for_processed_files & Report::processed_files()const 
{ 
    return processed_files_; // Returns by reference 
} 

そして、あなたはBEGを定義し、他の人が提起したとしても、他の問題があるかもしれません

typename Report::container_type_for_processed_files::const_iterator & beg = rep.processed_files().begin(); 
typename Report::container_type_for_processed_files::const_iterator & end = rep.processed_files().end(); 

を終了するとき、あなたは&を必要としています。たとえば、qDebugとは何ですか?

+0

ありがとう、それです。 – smallB