Dear experts,
I am receiving the following error with a small practice program I am writing to test my knowledge on pointers and operator overloading
Debug Assertion Failed!
Expression: _BLOCK_TYPE_IS_VALID(pHead
->nBlockUs
e)
Here is my class Rectangle and the main method which creates objects of it, the point at which the error occurs is before it prints the final 3 cout's (on the statement that utlizes the overloaded operator + for my Rectangle class). Why am I getting the error?
#include "stdafx.h"
#include <iostream>
using namespace std;
class Rectangle
{
private:
int * height, * width;
public:
Rectangle () {};
Rectangle(int, int);
~Rectangle();
int get_area(void);
int get_height(void);
int get_width(void);
Rectangle operator + (Rectangle);
};
Rectangle::Rectangle(int a, int b)
{
height = new (nothrow) int;
width = new (nothrow) int;
*height = a;
*width = b;
}
Rectangle::~Rectangle()
{
delete height;
delete width;
}
int Rectangle::get_area(void)
{
return get_height() * get_width();
}
int Rectangle::get_height(void
)
{
return *height;
}
int Rectangle::get_width(void)
{
return *width;
}
Rectangle Rectangle::operator +(Rectangle rect)
{
Rectangle retRect ((this->get_height() + rect.get_height()), (this->get_width() + rect.get_width()));
return retRect;
}
int _tmain(int argc, _TCHAR* argv[])
{
Rectangle rect1 (50, 4), *rect1Ptr;
rect1Ptr = &rect1;
cout << "rect 1 Height: " << rect1Ptr->get_height() << endl;
cout << "rect 1 Width: " << rect1Ptr->get_width() << endl;
cout << "rect 1 Area: " << rect1Ptr->get_area() << endl;
cout << endl;
Rectangle rect2 (20, 20);
cout << "rect 2 Height: " << rect2.get_height() << endl;
cout << "rect 2 Width: " << rect2.get_width() << endl;
cout << "rect 2 Area: " << rect2.get_area() << endl;
cout << endl;
Rectangle rect3;
rect3 = *rect1Ptr + rect2; // Error occurs on this line
cout << "rect 3 Height: " << rect3.get_height() << endl;
cout << "rect 3 Width: " << rect3.get_width() << endl;
cout << "rect 3 Area: " << rect3.get_area() << endl;
system("pause");
return 0;
}