Pointers to classes to structure ?

Hi Guys!
My task is to check if a circle belongs to a rectuangle using pointers, struct point and class okr; rect.
I think i finished the program but i got send/don`t send error.
I think the mistake is somewhere in the chek function.
I would be grateful if you manage to help me :)





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;
struct point{
	float x,y;
};
class okr {
public:
	point *O;
	float *r;
	okr(void);
	~okr(void);
};
class rect {
public:
	point *A;
	float *a,*b;
	rect(void);
	~rect(void);
	void chek(okr);
};
okr::okr(){
	O=new point;
	r= new float;
	cout<<"O.x=";cin>>O->x;
	cout<<"O.y=";cin>>O->y;
	cout<<"r=";cin>>*r;
}
rect::rect(){
	A=new point;
	a=new float;
	b=new float;
	cout<<"A.x=";cin>>A->x;
	cout<<"A.y=";cin>>A->y;
	cout<<"a=";cin>>*a;
	cout<<"b=";cin>>*b;
}
void rect::chek(okr k){

	if((k.O->x-*k.r)<(A->x+*k.r)&&((k.O->x+*k.r)<(A->x+*a))&&((k.O->x-*k.r)>A->x+*a)&&((k.O->x+*k.r)<A->x+*b)){
		cout<<"O.x="<<k.O->x;
		cout<<"O.y="<<k.O->y;
		cout<<"r="<<k.r;
	}
}
okr::~okr(){
	delete O;
	delete r;
}
rect::~rect(){
	delete A;
	delete a;
	delete b;
}
void main () {
	okr k;
	rect p;
	p.chek(k);
}
I think i finished the program but i got send/don`t send error.
what?

well, your program will crash. On line 37 the content of okr is copied (the pointers are copied). when rect::chek(okr k) ends the destructor of okr is called and that deletes the pointers 'O' and 'r'. But okr on line 55 still holds the (now invalid) poniters and when its destructor is called at the end of main() the invalid pointers are deleted again.

To avoid that you can write on line 37 void rect::chek(const okr &k){
Topic archived. No new replies allowed.