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:
// 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>
usingnamespace 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);
operatordouble();
operatorfloat();
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::operatorfloat()
{
twoDvector temp;
temp = arctan(dy/dx);
return temp;
}
twoDvector::operatordouble()
{
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:
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?
// 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>
usingnamespace 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);
operatordouble();
operatorfloat();
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::operatorfloat()
{
float temp;
temp = atan2(dy,dx);
return temp;
}
twoDvector::operatordouble()
{
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);
}
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.
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?
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
}
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?
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.
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.