このコンストラクターにスペースを割り当てる方法がわかりません。start と end の間に別の Vector の要素を含む Vector を構築することになっています。
template <class T> Vector<T>::Vector(const_iterator start, const_iterator finish)
{
array = new T[2 * capacity()];
for( ; start != finish; start++ ){
push_back(*start);
}
}
これをデバッグしようとしていますが、理解できません。push_back は次のようになります。
template <class T> void Vector<T>::push_back(const T & val)
{
if (size() == capacity()) {
reserve (2 * capacity() + 1);
}
array[ theSize++ ] = val;
}
どのコールが予約されていますか..
template <class T> void Vector<T>::reserve(int newCapacity)
{
if (newCapacity < size()) {
return;
}
T * array_copy = array;
array = new T [newCapacity];
for (int i = 0; i < size(); i++) {
array[i] = array_copy[i];
}
theCapacity = newCapacity;
delete [] array_copy;
}
したがって、質問を絞り込むには:
セグメンテーション違反が発生するのはなぜですか?