m_Arrayの削除には多くの問題があります。プログラムは、クリーンアップ部分を実行しているときに最後にセグメンテーションを行います。私は、m_Arrayに異なるデータを持つ2つのクラスAオブジェクトを持っています。そして、あるオブジェクトからのデータは、別の配列に "折り返し"始めて不正なデータを引き起こします。 Tは私のテンプレートデータ型を表します。C++ペアの動的なサイズの配列の削除に問題があります
ちょうど2つのクラスAのようなクラス宣言に公に宣言
オブジェクトを作成し、クラスBは、もあります:
template <typename T>
A<T>::A(int size) {
// Set array size
m_Array = new pair<T, int>[size];
// Initialize values
for (int i = 0; i < size; i++) {
m_Array[i] = make_pair(-1, -1);
}
//... other things defined and initialized...//
}
:ようなクラスAのコンストラクタ定義で定義された
template <typename T> class A
{
public:
pair<T, int> *m_Array; // Array of type pair
A(int size=1); // constructor
~A(); // A destructor
// ... all other definitions //
};
クラスAのデストラクタ:
template <typename T>
A<T>::~A() {
delete [] m_Array; // Not working as it should
}
オーバーロード代入演算子
template <typename T>
const A<T>& A<T>::operator=(const A<T>& rhs) {
m_AArraySize = rhs.m_AArraySize;
m_currASize = rhs.m_currASize;
for (int i = 0; i < m_currASize; i++) {
m_Array[i].first = rhs.m_Array[i].first;
m_Array[i].second = rhs.m_Array[i].second;
}
_ptr = rhs._ptr;
return *this;
}
コピーコンストラクタ
template <typename T>
A<T>::A(const A<T>& other) {
m_AArraySize = other.m_AArraySize;
m_AHeapSize = other.m_AHeapSize;
for (int i = 0; i < m_currASize; i++) {
m_Array[i].first = other.m_Array[i].first;
m_Array[i].second = other.m_Array[i].second;
}
_ptr = other._ptr;
}
クラスB宣言
template <typename T> class B{
public:
//B constructor
B(int size);
int m_currBSize; // spots used in array
int m_BSize; // size of array
A <T> oneAHolder;
A <T> twoAHolder;
};
クラスBコンストラクタ
template <typename T>
b<T>::b(int size){
A<T>(size);
m_BArraySize = size;
m_currBSize = 1;
// Create two A objects
A<T> oneA(size);
A<T> twoA(size);
// oneA and twoA go out of scope
oneAHolder = oneA;
twoAHolder = twoA;
}
私の主な機能では、クラスBオブジェクトを作成し、そのAオブジェクトの両方にデータを挿入する挿入機能を使用しています。
私は配列からデータを削除し、データを他の配列にあふれさせるのをやめさせましたが、役に立たなかったのです。
ご協力いただきありがとうございます。
PSは:なし
EDIT "だけのstd ::ベクトルを使用" してください:
1):あなたが投稿コードを考えると、私のコード
「ラップアラウンド」とはどういう意味ですか?それは私にとって二重の自由な問題のように聞こえる。あるオブジェクトを別の 'A newA = anotherA'に代入していますか?このhttps://stackoverflow.com/questions/7823845/disable-compiler-generated-copy-assignment-operatorを見たり、それらの機能を実装したりしてください。 –
あなたの 'main'プログラムを私たちに教えてください。あなたが投稿したもので、プログラムは2行のコードで簡単に壊れることがあります。 – PaulMcKenzie
"単にstd :: unique_ptrを使用してください" –