2

次の点を考慮してください。

// from main file

// first arguement gets enc_array
Rock rock (voice.getEncArray());
// getEncArray() gets a vector of vectors:
// std::vector<std::vector<unsigned int> > enc_array;

// in rock.hpp file, consider members
Rock( const std::vector<std::vector<unsigned int> > &);

std::vector<std::vector<unsigned int> * > remaining;

const std::vector<std::vector<unsigned int> > * population;

// in rock.cpp
Rock::Rock ( const vector<vector<unsigned int> > & v) :
  population (&v),
  ....

// in one of the class member functions
for ( vector<vector<unsigned int> >::const_iterator ci = population->begin(); ci != population->end(); ++ci ) {
    // for some indexes...
    remaining.push_back (& (*ci));      // <------  PROBLEM
}

gcc レポート:

error: no matching function for call to 'std::vector<std::vector<unsigned int>*>::push_back(const std::vector<unsigned int>*)'

note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<unsigned int>*; _Alloc = std::allocator<std::vector<unsigned int>*>; std::vector<_Tp, _Alloc>::value_type = std::vector<unsigned int>*] <near match>

note:   no known conversion for argument 1 from 'const std::vector<unsigned int>*' to 'std::vector<unsigned int>* const&'

非 constvector<int>と見なされたのアドレスをプッシュしようとしていることを理解しています。がいっぱいになった後、他のメソッドはそのデータを変更しないため、実際には. しかし、エラーが発生するため、 として宣言することはできません。constvectorremainingconstremainingconst vector

error: no matching function for call to 'std::vector<std::vector<unsigned int>*>::push_back(const std::vector<unsigned int>*) const'

note: candidate is:
note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::vector<unsigned int>*; _Alloc = std::allocator<std::vector<unsigned int>*>; std::vector<_Tp, _Alloc>::value_type = std::vector<unsigned int>*] <near match>

note:   no known conversion for argument 1 from 'const std::vector<unsigned int>*' to 'std::vector<unsigned int>* const&'

本当に からpopulationに要素をコピーする必要がありremainingますか? または、このオーバーヘッドが発生しないようにするために他にできることはありますか?

4

2 に答える 2

2

の要素のアドレスを取得していますが、population後で変更する可能性があります。populationと指定したため、これは悪いことですconst

の要素を変更する場合は、定義constからキーワードを削除してから、代わりに を使用する必要があります。populationiteratorconst_iteratorpopulation

于 2012-12-06T17:53:31.163 に答える
1

試す

std::vector<const std::vector<unsigned int> *> remaining;
于 2012-12-06T17:56:54.463 に答える