我正在使用Visual Studio。
我有3节课。 尝试打印 Rectangle ABCD 没关系,但是当我将ABCD推入 Array [rectangle] A 并尝试再次打印ABCD时,“ wntdll.pdb未加载”出现,我继续,它是〜Rectangle()
中 delete 处的“ 触发断点错误!”我知道这取决于Rectangle类中的指针,但无法说明问题。
`int main(){
Array<Rectangle>A;
Rectangle a;
cout << a; //It's oke
A.PushBack(a); // problem here
cout<<a;
}`
class Point
{
private:
float _x;
float _y;
public:
float GetX() { return _x; }
float GetY() { return _y; }
public:
Point();
Point(float,float);
Point(const Point&);
~Point() {};
public:
string ToString() const;
public:
friend istream& operator>>(istream&,Point*);
friend ostream& operator<<(ostream&,const Point&);
};
Point::Point() {
_x = 1;
_y = 1;
}
Point::Point(float x,float y) {
_x = x;
_y = y;
}
Point::Point(const Point& a) {
_x = a._x;
_y = a._y;
}
string Point::ToString() const{
stringstream out;
out << "( " << _x << "," << _y << " )";
return out.str();
}
istream& operator>>(istream& in,Point* a) {
cout << "Nhap x: ";
in >> a->_x;
cout << "Nhap y: ";
in >> a->_y;
return in;
}
ostream& operator<<(ostream& out,const Point& a)
{
out << a.ToString();
return out;
}
```
class Rectangle
{
private:
Point* _topleft;
Point* _botright;
public:
void Set_topleft(Point tl) { _topleft = &tl; }
Point Get_topleft() { return *_topleft; }
void Set_botright(Point br) { _botright = &br; }
Point Get_botright() { return *_botright; }
public:
Rectangle();
Rectangle(Point*,Point*);
~Rectangle();
public:
string ToString() const;
public:
friend istream& operator>>(istream&,Rectangle&);
friend ostream& operator<<(ostream&,const Rectangle&);
};
Rectangle::Rectangle()
{
_topleft = new Point(0,2);
_botright = new Point(3,0);
}
Rectangle::Rectangle(Point* a,Point* b)
{
_topleft = new Point(*a);
_botright = new Point(*b);
}
Rectangle::~Rectangle()
{
delete _topleft;
delete _botright;
}
string Rectangle::ToString() const
{
stringstream out;
out << "A" << *_topleft << "+" << "D" << *_botright;
return out.str();
}
istream& operator>>(istream& in,Rectangle& a)
{
std::cout << "A( x,y ): ";
in >> a._topleft;
std::cout << "D( x,y ): ";
in >> a._botright;
return in;
}
ostream& operator<<(ostream& out,const Rectangle& a)
{
out << a.ToString();
return out;
}
```
template<class T>
class Array
{
private:
T* _a;
int _len;
public:
Array();
~Array();
public:
int length() { return _len; }
void PushBack(T);
T Getat(int);
};
template<class T>
Array<T>::Array() {
_a = new T[128];
_len = 0;
}
template<class T>
Array<T>::~Array() {
delete[] _a;
_len = 0;
}
template<class T>
void Array<T>::PushBack(T value) {
if (_len >= 128)
{
std::cout << "Array is over size,which is 128\n";
return;
}
_a[_len] = value;
_len++;
}
template<class T>
T Array<T>::Getat(int pos) {
return _a[pos];
}
```