1

I have a simple vector class, and I'm trying to overload the operator+ to add vectors. When it didn't work in my main program, I created a new project and stripped it down to the bare minimum, and it still didn't work.

Bare minimum code:

main.cpp

#include "vector3f.h"

int main()
{
    Vector3f* a = new Vector3f();
    Vector3f* b = new Vector3f();
    Vector3f* c = a + b;
}

vector.h

#ifndef __VECTOR3F_H__
#define __VECTOR3F_H__

class Vector3f
{
public:
    float x;
    float y;
    float z;

    Vector3f();
    Vector3f(float x, float y, float z);
    ~Vector3f();

    Vector3f operator+(const Vector3f& rhs);
}; 

#endif

vector.cpp

#include "vector3f.h"

Vector3f::Vector3f()
{
    x = 0;
    y = 0;
    z = 0;
}

Vector3f::Vector3f(float x, float y, float z)
{
    this->x = x;
    this->y = y;
    this->z = z;
}

Vector3f::~Vector3f()
{

}

Vector3f Vector3f::operator+(const Vector3f& rhs)
{
    return Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
}

Here is the compile error:

main.cpp: In function 'int main()':
main.cpp:7: error: invalid operands of types 'Vector3f*' and 'Vector3f*' to binary 'operator+'

main.cpp line 7 is Vector3f* c = a + b;.

So my question, as is expected on Stack Overflow, is: what am I doing wrong?

Side note: I have a very lame IDE and the problem may be a faulty compiler, though I don't expect this to be the case.

Any help is appreciated. Thanks in advance!!

Joshua Hyatt
  • 1,300
  • 2
  • 11
  • 22

4 Answers4

3

You dynamically allocated your Vectors, and the + operator is not defined for pointers to vectors.

So you need to change:

 Vector3f* a = new Vector3f();
 Vector3f* b = new Vector3f();
 //... assigning of and b
 Vector3f* c = a + b;

To

Vector3f* a = new Vector3f();
Vector3f* b = new Vector3f();
Vector3f* c = new Vector3f();
*c = *a + *b;
AndyG
  • 39,700
  • 8
  • 109
  • 143
  • Sure, no problem :-). You may also want to define a copy constructor for your vector class so you can say `Vector3f* c = new Vector3f(*a + *b);` which may be more concise. – AndyG Mar 19 '14 at 05:34
  • Why the parenthesis around the *a and *b? Point me to some website? – Joshua Hyatt Mar 19 '14 at 05:35
  • I know more about Java than C++, so if my question is too stupid, disregard it. :) – Joshua Hyatt Mar 19 '14 at 05:35
  • It's a bit overkill on my part. The parenthesis were just to make sure that `a` and `b` were de-referenced before the `+` operation. I tend to overuse parenthesis to ensure order of operations instead of just looking up precedence. – AndyG Mar 19 '14 at 05:36
  • I removed the parenthesis. Operator precedence declares dereference before addition. – AndyG Mar 19 '14 at 05:37
2

I'm surprised this hasn't been said, but why are you using pointers at all?

Vector3f a;
Vector3f b;
Vector3f c = a + b;

No more trying to add pointers. No more awkward syntax. No more memory leaks.

Apart from this, you have some other things you should change:

  • __VECTOR3F_H__ is a reserved identifier.
  • You should get used to using constructor initializer lists (: x(0), y(0), z(0) and : x(x), y(y), z(z)) instead of assigning to data members after they've already been initialized.
  • You don't need a destructor here, as one will be provided for you.
  • operator+ should be implemented using operator+=, which you should also provide.
Community
  • 1
  • 1
chris
  • 60,560
  • 13
  • 143
  • 205
  • Pointers are awesome, dude. – Joshua Hyatt Mar 19 '14 at 05:40
  • @Josh, In what way? They add extra typing in order to do anything, have unclear ownership semantics, and can easily lead to memory leaks (or double frees, or deleting a stack-allocated object, or trying to dereference an invalid pointer). – chris Mar 19 '14 at 05:42
  • I actually don't know why I use pointers. Like I said in a previous comment, I don't know as much about C++ as I do about Java, where everything is gloriously pointer-free. – Joshua Hyatt Mar 19 '14 at 05:43
  • Also, thanks for the additional tips, I was hoping I would get those. :) – Joshua Hyatt Mar 19 '14 at 05:44
  • @Josh, Java still has "pointers". They're just called references and are garbage collected. You'd be wise to pick up some "C++ for Java Programmers" book. – chris Mar 19 '14 at 05:44
  • Java doesn't have so much explicit pointers like C++ does. That's what I meant. – Joshua Hyatt Mar 19 '14 at 05:45
1

Try this ,

 Vector3f c = *a + *b;

Now this will overload your operator else it was an pointer addition

balaji
  • 81
  • 6
1

You have overloaded objects not object pointers. Use this

Vector3f c = (*a) + (*b);

Dipika
  • 584
  • 2
  • 12
  • This will not work. You cannot assign a `Vector3f*` to a `Vector3f`. Lose the `*` on `c` – AndyG Mar 19 '14 at 05:33