안녕하세요 저는 연산자 오버로딩을 배우고있었습니다. 소멸자를 호출 할 때 프로그램이 중단 된 것으로 나타났습니다. 도움이 필요합니다. 감사소멸자를 호출 할 때 오류가 발생했습니다.
#include<iostream>
using namespace std;
class Overload
{
private:
int a, *b;
public:
Overload():a(0) {cout << "default\n"; };
Overload(int x);
Overload operator+(Overload & rhs);
int geta();
int* getb();
void setb();
void PrintVals(const Overload & val);
~Overload();
};
Overload::Overload(int x)
{
cout << "Parameterized constructor\n";
a = x;
b = new int[a];
}
int Overload::geta()
{
return a;
}
int* Overload::getb()
{
return b;
}
void Overload::setb()
{
int val;
cout << "setting b values\n";
for (int i = 0 ; i <= a; i++)
{
cin >> val;
b[i] = val;
}
}
Overload Overload::operator+(Overload & rhs)
{
Overload temp;
temp.a = this->a + rhs.a;
temp.b = new int[temp.a];
*temp.b = *(this->b) + *(rhs.b);
cout << "inside overload + vale of LHS:" << *(this->b) << endl;
cout << "inside overload + vale of RHS:" << *(rhs.b) << endl;
cout << "inside overload + vale of temp:" << *temp.b << endl;
cout << "Address of b(temp):" << temp.b << endl;
temp.b++;
this->b++;
rhs.b++;
*temp.b = *(this->b) + *(rhs.b);
cout << "inside overload + vale of LHS:" << *(this->b) << endl;
cout << "inside overload + vale of RHS:" << *(rhs.b) << endl;
cout << "inside overload + vale of temp:" << *temp.b << endl;
cout << "Address of b(temp):" << temp.b << endl;
return temp;
}
Overload::~Overload()
{
cout << "Destructor \n";
cout << "Address deallocated b:" << b;
delete [] b;
}
void Overload::PrintVals(const Overload & val)
{
int val1, *val2;
val1 = this->a;
val2 = this->b;
cout << "Printing values: a: " << val1 << " b:" << *val2;
}
int main()
{
Overload X(1),Y(1),Z;
int val1, *val, val2, val3;
//Z = X + Y;
val1 = Y.geta();
val2 = X.geta();
Y.setb();
val = Y.getb();
printf("val of y b: %d\n",*val);
printf("val of x a: %d\n",val1);
X.setb();
val = X.getb();
printf("val of x b: %d\n",*val);
printf("val of x a: %d\n",val2);
Z = X + Y;
val = Z.getb();
val3 = Z.geta();
val--;
for(int i = 0; i < val3; i++)
{
printf("address of (b) Z: %p \n",val);
printf("val of z b: %d\n",*val);
printf("val of z a: %d\n",val3);
val++;
}
}
출력 : ... 소멸자 1 부 (4815,0x7fff73def300)의 malloc : * 객체 0x100200004에 대한 오류 : 포인터가 * 디버깅 malloc_error_break에 중단 점을 설정 할당되지 않은 해제되지 주소 할당 취소 된 b : 0x100200004 (lldb)