0
using namespace std;

template <typename A>
class vector3d_add
{
    public:
            vector3d_add(A x, A y, A z);
            virtual ~vector3d_add();
            void setX(A x);
            void setY(A y);
            void setZ(A z);
            void display();
                  A getX();
                  A getY();
                  A getZ();
            A operator+ (const A& v2);
            A& operator+= (const A& v2);

    private:
             A x;
             A y;
             A z;
};


template <typename A>
vector3d_add<A>::~vector3d_add()
{
    cout << "deleted" << endl;
}

template <typename A>
vector3d_add<A>::vector3d_add(A x, A y, A z)
{
    this->x = x;
    this->y = y;
    this->z = z;
}

template <typename A>
A& operator+=(const A& v2) //x1
{
    this->x += v2.x;
    this->y += v2.y;
    this->z += v2.z;
    return *this;
}

template <typename A>
A operator+(const A& v2)
{
    return A(*this) += v2; //x2
}

template <typename A>
void vector3d_add<A>::display()
{
    cout<<this->x<<endl<<this->y<<endl<<this->z<<endl;
}

int main()
{
    VECTOR<int>v1(10,10,10);
    VECTOR<int>v2(10,10,10);
    VECTOR<int>v3=v2+v1;

    v3.display();
}

I want to realize a code, that can add 3D vector with template and operator overloading (+operator and +=operator MUST BE USED). I don't know how to get any further.

I searched at the internet for other solution, but nodody was using the +operator AND the +=operator.

I tried other things, to realize it, but i dont get it.

ERORRS:
x1->'A& operator+=(const A&)' must take exactly two arguments x2->invalid use of 'this' in non-member function

  • 1
    *I searched at the internet for other solution, but nodody was using the +operator AND the +=operator.* -- You really didn't search hard enough, since almost all well-written code that uses `+` has both `+` and `+=` overloaded. Hint -- start by writing `operator +=` first, not `operator +`. Then work from there. – PaulMcKenzie Oct 05 '20 at 17:06
  • Thanks for the fast answer, but I dont really know, how to program the +=operator and I need someone who can check my code, if it would work. – Observer101 Oct 05 '20 at 17:11
  • `I need someone who can check my code, if it would work.` and `// Dontknow how to do this overloading` don't explain what your problem is. Do you get an error message when building? What have you tried at `// Dontknow how to do this overloading`? – t.niese Oct 05 '20 at 17:13
  • I'm pretty sure the vec3 implementation of `glm`, `Eigen` and others all use ` +=operator` and `+operator`. – t.niese Oct 05 '20 at 17:15
  • And there is this question [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706) with this [answer](https://stackoverflow.com/a/4421719) with the section **Binary arithmetic operators** with an example for `operator +` and `operator +=`. – t.niese Oct 05 '20 at 17:20
  • @Observer101 I posted an answer. Did it solve your problem? – AKL Oct 05 '20 at 17:20
  • I have 2 errors. I posted it under the top answer – Observer101 Oct 05 '20 at 17:23
  • @Observer101 you have to include the errors that you have with **your** code in the question so that it is clear what problem you have. – t.niese Oct 05 '20 at 17:25
  • Updated the code. Sorry im really new here, dont know, how everything exactly works here – Observer101 Oct 05 '20 at 17:31
  • I posted a complete answer. Let me know if it solves your problem. – AKL Oct 05 '20 at 18:05
  • @Observer101 -- Your code is inconsistent, as the body of `operator +` suggests that you are being passed a `vector3d_add` object, not an `A` object. – PaulMcKenzie Oct 05 '20 at 18:08

2 Answers2

0

There are too many errors in your code.

First never use using namespace std;

Second why the return types of the operators are A. they should be vector3d_add<A>!

Here is a working code:

template <typename A>
class vector3d_add
{
public:
    vector3d_add(A x, A y, A z);
    virtual ~vector3d_add();
    void setX(A x);
    void setY(A y);
    void setZ(A z);
    void display();
    A getX();
    A getY();
    A getZ();
    vector3d_add operator+ (const vector3d_add& v2);
    vector3d_add& operator+= (const vector3d_add& v2);

    private:
             A x;
             A y;
             A z;
};


template <typename A>
vector3d_add<A>::~vector3d_add()
{
    std::cout << "deleted" << std::endl;
}

template <typename A>
vector3d_add<A>::vector3d_add(A _x, A _y, A _z):x(_x),y(_y),z(_z){}

template <typename A>
vector3d_add<A>& vector3d_add<A>::operator+=(const vector3d_add<A>& v2) //x1
{
    x += v2.x;
    y += v2.y;
    z += v2.z;
    return *this;
}

template <typename A>
vector3d_add<A> vector3d_add<A>::operator+(const vector3d_add<A>& v2)
{
    vector3d_add<A> result(*this);
    result.x += v2.x;
    result.y += v2.y;
    result.z += v2.z;
    return result; //x2
}

template <typename A>
void vector3d_add<A>::display()
{
    std::cout<<this->x<<std::endl<<this->y<<std::endl<<this->z<<std::endl;
}

apply the very same operator for each member and in the end return a reference of itself.

Usage:

int main()
{
    vector3d_add<int> a1(1,2,3);
    vector3d_add<int> a2(4,5,6);
    vector3d_add<int> a3 = a1 + a2;
    a1 += a2;
    a1.display();
    a3.display();
    return 0;
}
AKL
  • 1,367
  • 7
  • 20
  • @Observer101 thank you for your question. If my answer helped please consider accepting it by clicking on the tick mark beside my answer – AKL Oct 06 '20 at 15:04
0

First, the return value of operator + should be vector3d_add&, not A. Given the body of the function you wrote, one can assume that you are trying to change the existing vector3d_add<T> object.

Given this, instead of starting out writing operator +, write the operator += first. Then once that's written, you can write operator + in terms of operator +=:

template <typename A>
class vector3d_add
{
    public:
            vector3d_add(A x, A y, A z);
            virtual ~vector3d_add();
            void setX(A x);
            void setY(A y);
            void setZ(A z);
            void display();
                  A getX();
                  A getY();
                  A getZ();
            vector3d_add<A>& operator+=(const vector3d_add<A>& v2);
            vector3d_add<A> operator+(const vector3d_add<A>& v2);

    private:
             A x;
             A y;
             A z;
};

template <typename A>
vector3d_add<A>& vector3d_add<A>::operator+=(const vector3d_add<A>& v2)
{
    x += v2.x;
    y += v2.y;
    z += v2.z;
    return *this;
}

template <typename A>
vector3d_add<A> vector3d_add<A>::operator+(const vector3d_add<A>& v2)
{
    return vector3d_add(*this) += v2;
}

The operator + simply creates a temporary copy from this, and then invokes += on the temporary copy. Then that result is returned.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45