Note that there are some explanatory texts on larger screens.

plurals
  1. POI'm having trouble with operator overloading of the operator- function
    text
    copied!<p>The compiler gives me an error when trying to subtract two Point3D objects. I get this error:</p> <pre><code>Invalid operands to binary expression ('Point3D' and 'Point3D') </code></pre> <p>This is what I have in my Vector3D.h:</p> <pre><code>#include "Point3D.h" using namespace std; class Vector3D { friend const Point3D operator+(const Point3D&amp; a, const Vector3D&amp; b); friend const Vector3D operator-(const Point3D&amp; a, const Point3D&amp; b); public: Vector3D() {} Vector3D(float x, float y, float z); Vector3D(Point3D const&amp; originPoint, float theta, float distance); float getX() const {return x;} float getY() const {return y;} float getZ() const {return z;} static Vector3D minus(Point3D const&amp; destination, Point3D const&amp; origin); Vector3D operator-(Vector3D const&amp; other) const; float dot(Vector3D const&amp; other) const; static float angleBetweenTwoVectorsZeroToPi(Vector3D const&amp; a, Vector3D const&amp; b); static float angleBetweenTwoVectorsZeroToTwoPi(Vector3D const&amp; a, Vector3D const&amp; b); Vector3D normalize() const; float length() const; //const float * const getArray() const {return &amp;x;} Vector3D multiply(float scalar) const; bool operator==(Vector3D const&amp; v) const; float operator[] (int i) const; private: float x; float y; float z; }; </code></pre> <p>The Vector3D.cpp file which defines the binary operators:</p> <pre><code>#include "Vector3D.h" #include "Math3D.h" #include &lt;math.h&gt; #include "MathConstants.h" Vector3D::Vector3D(float x, float y, float z): x(x), y(y), z(z) {} Vector3D::Vector3D(Point3D const&amp; originPoint, float theta, float distance) { Point3D endPoint = Math3D::calcaultePoint3D(originPoint, theta, distance); Vector3D result = minus(endPoint, originPoint); this-&gt;x = result.x; this-&gt;y = result.y; this-&gt;z = result.z; } Vector3D Vector3D::minus(Point3D const&amp; destination, Point3D const&amp; origin) { return Vector3D(destination.getX() - origin.getX(), destination.getY() - origin.getY(), destination.getZ() - origin.getZ()); } Vector3D Vector3D::operator-(Vector3D const&amp; other) const { return Vector3D(x-other.x, y-other.y, z-other.z); } float Vector3D::dot(const Vector3D &amp;other) const { return x * other.x + y * other.y + z * other.z; } float Vector3D::length() const { return sqrtf(dot(*this)); } Vector3D Vector3D::normalize() const { float len = length(); return Vector3D(getX()/len, getY()/len, getZ()/len); } Vector3D Vector3D::multiply(float scalar) const { return Vector3D(x * scalar, y * scalar, z * scalar); } float Vector3D::angleBetweenTwoVectorsZeroToPi(const Vector3D &amp;a, const Vector3D &amp;b) { /* * The result is between 0 and PI */ Vector3D unitA = a.normalize(); Vector3D unitB = b.normalize(); return acos(unitA.dot(unitB)); } bool Vector3D::operator==(const Vector3D &amp;v) const { return (x == v.x) &amp;&amp; (y == v.y) &amp;&amp; (z == v.z); } float Vector3D::operator[](int i) const { return (&amp;x)[i]; } float Vector3D::angleBetweenTwoVectorsZeroToTwoPi(const Vector3D &amp;a, const Vector3D &amp;b) { /* * The result is between 0 and 2PI * * "Assuming a = [x1,y1] and b = [x2,y2] are two vectors with their bases at the * origin, the non-negative angle between them measured counterclockwise * from a to b is given by * * angle = mod(atan2(x1*y2-x2*y1,x1*x2+y1*y2),2*pi); * * As you can see, this bears a close relationship to the three-dimensional * formula I wrote last July 10. The quantities, x1*y2-x2*y1 and x1*x2+y1*y2 * are, respectively, the sine and cosine of the counterclockwise angle from * vector a to vector b, multiplied by the product of their norms - that is, their * cross product and the dot product restricted to two dimensions. The 'atan2' * function then gives the angle between them ranging from -pi to +pi, and the * 'mod' operation changes this so as to range from 0 to 2*pi, as you requested." * * Roger Stafford * http://www.mathworks.com/matlabcentral/newsreader/view_thread/151925 */ float resultNegPiToPosPi = atan2f(a.x*b.y-b.x*a.y, a.x*b.x+a.y*b.y); if (resultNegPiToPosPi &lt; 0.0f) { resultNegPiToPosPi = resultNegPiToPosPi + 2*MathConstants::PI; } return resultNegPiToPosPi; } const Point3D operator+(const Point3D&amp; a, const Vector3D&amp; b) {return Point3D(a.getX()+b.getX(), a.getY()+b.getY(), a.getZ()+b.getZ());} const Vector3D operator-(const Point3D&amp; a, const Point3D&amp; b) {return Vector3D(a.getX()-b.getX(), a.getY()-b.getY(), a.getZ()-b.getZ());} </code></pre> <p>This is where I try to subtract a Point3D from another:</p> <pre><code>void AnimationService::handlePlayerMovement(double lastTime, double currentTime, Vector3D vector) { Point3D a; Point3D b; Vector3D result = a - b; // this is the problem line } </code></pre> <p>The strange part is that the binary <code>operator+</code> does work, but for some reason <code>operator-</code> gives me the error. Can anyone tell me what I'm doing wrong?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload