deconstructor run time

Hi everyone ,i wrote the following code where i a use a class to declare a friend function of overloaded operator +.Inside my class i have a pointer and although my code runs just fine when i add inside the class a deconstructor then i have a run time error.Take a look at the code:

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
#include<iostream>
using namespace std;

class one
{
	public:
		one(){a=new int;}
		one(int k,int l){a=new int;*a=k;b=l; }
	//~one(){delete a;a=NULL;} Here is the reason i have run time   error

		void ektiposi(){cout<<"a:"<<*a<<",b:"<<b;}
		friend const one operator+(const one& left,const one& right);

	private:
		int *a,b;
};

const one operator+(const one& left,const one& right)
{
  one temp;
  temp.b=left.b+right.b;
 *temp.a=*left.a;
 *temp.a+=*right.a;

 return temp;
}

int main()
{
	one n1(5,6),n2(4,2);

	one n3;
	n3=n1+n2;
	n3.ektiposi();


	cin.get();
	return 0;
}



I guess i create a conflict in memory using ~one(){delete a;a=NULL;} but the default deconstructor does the same thing,it destroys the allocated memory,right?Thanks
Hi there,

try this version:
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
#include<iostream>
using namespace std;

class one
{
	public:
		one():a(new int),b(0) {};
		one(int k, int l):a(new int),b(1) { *a = k; }
		~one() { delete a; a = NULL; }
		
		void ektiposi() { std::cout << "a:" << *a << ",b:" << b; }
		friend const one operator+(const one& lhs, const one& rhs);
		
	private:
		int* a;
		int  b;
};

const one operator+(const one& left,const one& right)
{
	one temp;
	
	*temp.a = (*left.a + *right.a);
	temp.b = (left.b + right.b);
	
	return temp;
}

int main()
{
	one n1(5,6),n2(4,2);

	one n3;
	n3=n1+n2;
	n3.ektiposi();


	cin.get();
	return 0;
}


I believe the issue is because you declared both a and b as pointers inadvertently when you meant to declare one pointer and one value. The above should fix your problem as this works for me now. Let me know if you still have problems.

Many thanks,

Phil.
Dear phil thanks for your time, actually i tried your version and still makes the same run time error.Well i don't know ,maybe my code is a mess :D
Hi there,

Your code is not a mess - I have seen far worse! :)

I apologize but I should have picked this up before - there was a second problem in your code which was on line 34. You use an assignment operator to assign the values of the unary operator to your class. As you had not specified one yourself the compiler generated one for you (it does this) which had an incorrect implementation. If you define an assignment operator for your class it solves the problem.

I have posted the code below and run it a few times to check that it works... :)
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
60
61
#include<iostream>
using namespace std;

class one
{
	public:
		one():a(new int),b(0) {};
		one(int k, int l):a(new int),b(1) { *a = k; }
		~one() { if(a != NULL) delete a; a = NULL; }

		one& operator=(const one& rhs)
		{
			// check for self assignment...
			if(this == &rhs)
				return *this;
				
			// clear previous values...
			if(a != NULL)
			{
				delete a;
			}
			
			// assignment...
			a = new int;
			*a = *rhs.a;
			b = rhs.b;
			
			// return..
			return *this;
		}
		
		void ektiposi() { std::cout << "a:" << *a << ",b:" << b; }
		friend const one operator+(const one& lhs, const one& rhs);
		
	private:
		int* a;
		int  b;
};

const one operator+(const one& left,const one& right)
{
	one temp;
	
	*temp.a = (*left.a + *right.a);
	temp.b = (left.b + right.b);
	
	return temp;
}

int main()
{
	one n1(5,6),n2(4,2);

	one n3;
	n3=n1+n2;
	n3.ektiposi();


	cin.get();
	return 0;
}

Here is a link to something that you might find useful in future projects:
http://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)

I hope this helps you.

Regards,

Phil.
Thanks Phil,indeed the code runs just fine! The assignment operator was the missing element.I guess that when i ad the deconstructor a new conflict rises through the increment n1+n2. Maybe the element (n1+n3) which is created after the add destroys the memory of its pointer a by calling the decostructor before the assignment is passed to the element n3 .Maybe that's why the operator assignment solves this problem . Maybe,i don't know... Thanks again :)
Last edited on
Topic archived. No new replies allowed.