Overloading typecasts

Hello everyone! I am currently writing a program that adds and subtracts vectors, by overloading the + and - operators. Next I have to overload the float typecast to return the value of r(=sqrt(x2+y2). Then I have to overload the float typecast to return the value of theta which is equal to the arctan(y/x). I am having some problems overloading these typecasts and getting a lot of errors when I do. If someone could help me with this that would be great! Thanks for any future help! Here is what I have so far:

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
// The purpose of this program is to overload the + and -
// operators in order to add and subtract 2 vectors. Then I will overload
// the double operator to return the value of r for the new vector.
// Then I will overload the float operator to return the value of
// theta for the new vector.

#include <iostream>
#include <cmath>
using namespace std;

class twoDvector {
private:
	double dx;
	double dy;
public:
	double getX() const {return dx;}
	double getY() const {return dy;}
	void setData(double x, double y) {x=dx; y=dy;}
	twoDvector operator +(const twoDvector &right);
	twoDvector operator -(const twoDvector &right);
	operator double();
	operator float();
	twoDvector(int x=1, int y=1) {setData(x,y);}
};

twoDvector twoDvector::operator +(const twoDvector &right)
{
	twoDvector tempVec;
	tempVec.dx = dx + right.dx;
	tempVec.dy = dy + right.dy;
	return tempVec;
}

twoDvector twoDvector::operator -(const twoDvector &right)
{
	twoDvector tempVec;
	tempVec.dx = dx + right.dx;
	tempVec.dy = dy + right.dy;
	return tempVec;
}

twoDvector::operator float()
{
	twoDvector temp;
	temp = arctan(dy/dx);
	return temp;
}

twoDvector::operator double()
{
	twoDvector temp;
	temp = (sqrt((dx*dx) + (dy*dy));
	return temp;
}


int main()
{
	twoDvector vector1(2,2), vector2(3,3);
	twoDvector vector3;

	vector3 = vector1+vector2;
	int(vector3);
	double(vector3);
	
}




Well your syntax problem is that your 'temp' object in your casting operators is a twoDvector and not a float/double as it is supposed to be.

Although...

This is a TERRIBLE idea. It is very easy to cast by accident and get a value that isn't at all what you expect. What's worse, you have float and double casts (two very similar types) doing completely different things. This will cause nothing but trouble for you.

Screw the typecasting. Just make these member functions:

1
2
3
4
5
6
7
class twoDvector
{
public:
  double arctan() const { return atan2(dy,dx); }
  double magnitude() const { return sqrt( (dx*dx) + (dy*dy) ); }

//... 
Normally I would agree with you Disch however, I have to do this problem for a course of mine and my instructor wants me to overload these typecasts to return those values that I said previously. Is there anyway you could help me with the overloading of the typecasts?
Here is what I have now..but how do I print out the overloading typecasts in main correctly?

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
// The purpose of this program is to overload the + and -
// operators in order to add and subtract 2 vectors. Then I will overload
// the double operator to return the value of r for the new vector.
// Then I will overload the float operator to return the value of
// theta for the new vector.

#include <iostream>
#include <cmath>
using namespace std;

class twoDvector {
private:
	double dx;
	double dy;
public:
	double getX() const {return dx;}
	double getY() const {return dy;}
	void setData(double x, double y) {x=dx; y=dy;}
	twoDvector operator +(const twoDvector &right);
	twoDvector operator -(const twoDvector &right);
	operator double();
	operator float();
	twoDvector(int x=1, int y=1) {setData(x,y);}
};

twoDvector twoDvector::operator +(const twoDvector &right)
{
	twoDvector tempVec;
	tempVec.dx = dx + right.dx;
	tempVec.dy = dy + right.dy;
	return tempVec;
}

twoDvector twoDvector::operator -(const twoDvector &right)
{
	twoDvector tempVec;
	tempVec.dx = dx + right.dx;
	tempVec.dy = dy + right.dy;
	return tempVec;
}

twoDvector::operator float()
{
	float temp;
	temp = atan2(dy,dx);
	return temp;
}

twoDvector::operator double()
{
	double temp;
	temp = (sqrt((dx*dx) + (dy*dy)));
	return temp;
}


int main()
{
	twoDvector vector1(2,2), vector2(3,3);
	twoDvector vector3;

	vector3 = vector1+vector2;
	int(vector3);
	double(vector3);
	
}
What you have there works except for 3 things:

line 63: you don't have an int overload. I assume you meant to cast to float there?

lines 63, 64: just cout those if you want to print them

line 18: take a very close look at this function....
I made the changes for what you said however I get some weird numbers instead of what I was supposed to get. Is that because of my setData function in line 18? If so, I don't see what is wrong with it and I also don't see why I am getting these weird numbers for answers too.
Last edited on
yes the problem is with setData.

Look closer. The function is totally broken. It doesn't do anything.
Ok i am just started learning how to do this stuff so I am kind of at a loss of how to fix this problem. I apologize for asking this cause I know the purpose of these forums are to learn and to do it yourself and then ask questions but I have no idea why it isn't do anything. How would I fix it in order to transfer values from my constructor to the setData function?
Last edited on
Oh my, but you must learn to debug! ;D

Let me put it this way:

1
2
3
4
5
6
7
8
int a = 0;
int b = 1;

a = b;  // what does this do?

// what will these output?
cout << a;
cout << b;


Pretty simple, right?

But now look at this:

1
2
3
4
5
6
7
8
9
10
11
12
class twoDvector {
private:
	double dx;
	double dy;
//...
	void setData(double x, double y)
	{
		// what does this do?
		x=dx; // what are x and dx after this line here?

		y=dy; // same
	}
Last edited on
Ok i see now what you mean. However, I am setting the values in the constructor and then passing them to the setData function. Wouldn't that make the setData function have values?
Ok i see now what you mean


I don't think you do...

*tries really hard not to completely spell it out*

okay... how about this...

- x is your function parameter, right?
- dx is your member variable, right?

Now look at this:
 
x=dx;


what's wrong here?

Explain to me in English what x=dx; does.
Last edited on
Haha sorry lol still pretty new at this! But now I hope this is right...I should switch it around so that the member variable dx equals the function parameter x right?? This way the member variable takes on the value of the function parameter, and the function parameter doesn't take on the value of the member variable dx which has no value.
Last edited on
correct!

You have the same problem with dy/y too.

You never set 'dx' to anything. Instead you're taking dx and putting it in your function parameter x (which is pointless because x dies as soon as the function exits). dx, and thus your twoDvector, remain unchanged by the function. The function therefore does nothing.


I'm glad I could help. You strike me as someone that actually wants to learn this stuff, not like someone who's just trying to get through the class.

Keep up the good work.
Yea thanks for all your help!
Topic archived. No new replies allowed.