errors get thrown when i close the cmd

i realize that i have coded more than i should for such a simple thing...but it was to just make sure i understood everything. what it does is add 2 vectors together. when adding, i create a temporary pointer to a Vector object. I store the added x and y values here and pass this pointer on to the Vector object which it is equaling to (see my custom operator= ?). i retrieve the new x and y values from the object via the pointer, then delete the pointer (Which in turn deletes the temporary object, yes? thats the whole point of my elaborate code so i hope so).

it all works fine. in the end it prints out 4,4. correct! however, when i close the program i get this:

http://img34.imageshack.us/img34/1866/16963809.png

it claims the error happens at

1
2
3
4
Vector::~Vector(){
  delete x;
  delete y;
}


but i cant see why because when we deleted our pointer earlier on (as a part of the plusing of the vectors), we didnt have any problems huh?

also 2nd question: i am using getters and setters to set the values of my x and y's since they are pointers. is their a way to access them directly from outside of the object? (things like object.*x returned errors?)

the code for this is as follows:


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
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <math.h>
using namespace std;

class Vector{
public:
  int *x,*y;
  Vector();
  Vector(int, int);
  ~Vector();
  int get_x();
  int get_y();
  void set_x(int);
  void set_y(int);
  Vector * operator + (Vector);
  void operator = (Vector *);
};

Vector::Vector() {
  x = new int;
  y = new int;
  *x = 0;
  *y = 0;
}

Vector::Vector(int xx, int yy) {
  x = new int;
  y = new int;
  *x = xx;
  *y = yy;
}
Vector::~Vector(){
  delete x;
  delete y;
}

int Vector::get_x(){
  return (*x);
}

int Vector::get_y(){
  return (*y);
}

void Vector::set_x(int xx){
  *x = xx;
}

void Vector::set_y(int yy){
  *y = yy;
}

Vector * Vector::operator + (Vector other) {
  Vector *temp;
  temp = new Vector;
  temp->set_x(*x + other.get_x());
  temp->set_y(*y + other.get_y());
  return(temp);
}

void Vector::operator = (Vector * other) {
  set_x(other->get_x());
  set_y(other->get_y());
  delete other;
}

int main () {
  Vector a,b(2,2),c(2,2);
  a = b + c;
  cout << a.get_x() << "," << a.get_y() <<"\n";
  return 0;
}
The allocations and deallocations look fine for me, and it also runs fine for me.
Things you should avoid:
* Using more pointers than necessary. It's more space-efficient and less error prone.
* Returning pointers from overloaded operators is generally a bad idea. For example, when you add two things, the last thing you'd think is that you later have to delete the result.
* The typical prototype for an overloaded operator=() is T &operator=(const T &);. The overload should return *this. Try not to change that, since people assume that behavior.
* Adding zero-depth interfaces (getters and setters that do nothing but return or set a member) usually -- and I emphasize "usually" -- means the member should be public. Adding interfaces for public members is entirely redundant. To answer your question, you can dereference those pointers with *object.pointer.
* using namespace.

Here's how the program would look properly written:
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
#include <iostream>

struct Vector{
	int x,y;
	Vector():x(0),y(0){};
	Vector(int a, int b):x(a),y(b){};
	Vector(const Vector &v){
		*this=v;
	};
	Vector operator+(Vector &);
	Vector &operator=(const Vector &);
};

Vector Vector::operator+(Vector &other) {
	Vector temp=*this;
	temp.x+=other.x;
	temp.y+=other.y;
	return temp;
}

Vector &Vector::operator=(const Vector &other) {
	x=other.x;
	y=other.y;
	return *this;
}

int main (){
	Vector a,b(2,2),c(2,2);
	a = b + c;
	std::cout << a.x << "," << a.y <<"\n";
	return 0;
}
i dont fully understand that example with what little i know...but i can sort of see what it does.

1 question though; dont we eventually get a huge accumulation of Vector temp's. with my code what i was trying to do was get rid of the new temporary vector objects i used.

added:

i may as well add my other questions...;

how comes the Vector &Vector::operator=(const Vector &other) returns *this ? where is the returned value ever used?

why is the type returned Vector & (the & (address of..?))?

infact you use Vector & a couple of times in different places throughout. what does it mean exactly? for example, when adding b + c, is c's address passed to b's operator+ function?
Last edited on
dont we eventually get a huge accumulation of Vector temp's.
I'll answer your question with another question: given the following function:
1
2
3
4
int f(){
    int a=1024;
    return a;
}

do ints continue to accumulate for all time? The answer to this question is the answer to your question.
the way im seeing it, when we create an int a, we need to store this somewhere in the memory. if we then run the function again we create another a which we store somewhere else and so on. i guess im wrong? if so i cant see where the memory cell storing the value for that "a" is deallocated space
The answer is no. When the function returns, all local data is discarded except for the return value, which is first copied to an intermediate location (where exactly will be determined by the compiler) and later copied to the variable or object that receives it. If the return value is ignored, it's discarded as well after returning.

Remember: Nothing that is created on the stack stays there after the function returns, not even the return value. Using heap allocation to avoid wasting memory like that is nonsensical.
However, a compelling reason to return allocated pointers is not to waste time copying large structures. You won't know when to use each method for a while. When in doubt, return by value, not by pointer.
Last edited on
thanks, though im still not understanding why the &'s are use'd..

here: Vector Vector::operator+(Vector &other) as the parameter

and here: Vector &Vector::operator=(const Vector &other) as the parameter and type. i assumed that the b that is passed into the operator+ function in, say, :

a + b

would be of Vector type, not Vector&

?
You'll find out soon enough.
well i went ahead and re-applied what i had learnt here to create some application that allows you to add together libraries. (the books and staff..). same concept, just used different words etc so i wasnt doing an exact copy :p

i did not use the & in the places you had used, but nevertheless i have got the same results..

getting a much better understanding of how pointers and all interact with each other.

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

struct Library{
  int books,staff;
  string name;
  Library():books(0),staff(0),name("untitled"){};
  Library(int a,int b,string c):books(a),staff(b),name(c){};
  void print_details();
  Library operator=(Library);
  Library operator+(Library);
};

Library Library::operator =(Library other){
  books = other.books;
  staff = other.staff;
  name = other.name;
  return *this;
}

Library Library::operator+(Library other){
  Library temp;
  temp.books = this->books + other.books;
  temp.staff = this->staff + other.staff;
  return temp;
}

void Library::print_details(){
  cout << "-------Library Details-------\nName: " << name << "\nBooks: "
  << books << "\nStaff: " << staff <<"\n---------------------\n";
}
int main () {
  Library a(10,1,"A"),b(100,5,"B"),c;
  a.print_details();
  c = a + b;
  c.print_details();
  return 0;
}
Last edited on
Topic archived. No new replies allowed.