m_Array 삭제시 많은 문제가 있습니다. 프로그램은 클린업 부분을 수행 할 때 마지막에 segfault합니다. m_Array에 다른 데이터가있는 두 개의 클래스 A 객체가 있고 프로그램의 어느 시점에서 한 객체의 데이터가 다른 배열로 "둘러싸여"시작되어 잘못된 데이터가 발생합니다. T는 내 템플릿 데이터 유형을 나타냅니다.C++ 쌍의 동적 크기 배열 삭제 중 문제가 발생했습니다.
두 클래스 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
}
,515,
오버 할당 연산자
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 "그냥 표준 : : 벡터를 사용하여"하십시오
1) : 당신이 게시 된 코드를 감안할 때 내 코드
을 의미합니까? 나에게 두 번 더 자유로운 문제처럼 들린다. 하나의 객체 A를 다른'A newA = anotherA'에 할당하고 있습니까? 이 https://stackoverflow.com/questions/7823845/disable-compiler-generated-copy-assignment-operator를 보거나 이러한 기능을 구현하십시오. –
'main' 프로그램을 보여주십시오. 게시 한 내용을 사용하면 프로그램을 두 줄의 코드로 쉽게 분리 할 수 있습니다. – PaulMcKenzie
그런데 –