Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Overload + operator for list of selfmade vector objects

I implemented a class for 2D Vectors and an addition operator (element wise) for it. What I try to achieve is a second class whose elements are supposed to be a list of said 2D-vectors. What I am having trouble with is overloading the addition operator, such that I can add two of those lists element wise (not append one to the other).

In other words: I am able to do the following things:
Let a and b be vectors. a+b is a vector as well. Let list_a be a list of n such vectors [a_1,a_2,...,a_n]
Same for list_b = [b_1,b_2,...,b_n].

What I fail at:
list_c = list_a + list_b in the sense that list_c = [a_1+b_1, a_2+b_2,..., a_n+b_n]

Here is a minimal non working example of what I did so far.

#include <iostream>
using namespace std;
class vec{
  public:
    double x[2];
    vec() {
        x[0] = 0; x[1] = 0;
    }
    vec(double a, double b) {
        x[0] = a; x[1] = b;
    }
    vec& operator+=(const vec& other){
        x[0] += other.x[0]; x[1] += other.x[1];
        return *this;
    }
};
vec operator+(vec a, vec b) {
    return vec(a.x[0] + b.x[0], a.x[1] + b.x[1]);
}
ostream &operator<<(std::ostream &os, vec const &v) {
    return os << v.x[0] << " " <<v.x[1];
}

class vec_list{
  private:
    int length;
  public:
    vec* entry;

    vec_list(vec* elements, int n){
        length = n; entry = elements;
    }
    void print(){
        cout<<"======="<<endl;
        for(int i = 0; i<length; i++){
            cout<<"["<<entry[i]<<"]"<<endl;
        }
        cout<<"======="<<endl;
    }
    vec operator [](int i) const    {return entry[i];}
    vec & operator [](int i) {return entry[i];}
    int len(){
        return length;
    }
};

vec_list operator+(vec_list a,vec_list b){
    int len = a.len();
    if(len == b.len()){
        vec result[len];
        for(int i = 0; i<len; i++){
            result[i] = a[i]+b[i];
            cout <<"["<<a[i]<<"] + ["<<b[i]<<"] = ["<<result[i]<<"]"<<endl; //print the element wise sum to check
        }
        return vec_list(result,len);
    }
    else{
        exit(1);
    }
}

int main(){
    int num = 3;
    vec vectors_a[num], vectors_b[num];
    for(int i=0; i < num; i++){
        vectors_a[i]=vec(i, i+1.1);
        vectors_b[i]=vec(i-0.2, i);
    }
    vec_list list_a = vec_list(vectors_a,num), list_b = vec_list(vectors_b,num);
    list_a.print();
    list_b.print();

    vec_list list_c = list_a+list_b;
    list_c.print();
}

What I expect would be:

=======
[0 1.1]
[1 2.1]                               <- The first list of vectors
[2 3.1]
=======
=======
[-0.2 0]
[0.8 1]                               <- The second list of vectors
[1.8 2]
=======
[0 1.1] + [-0.2 0] = [-0.2 1.1]
[1 2.1] + [0.8 1] = [1.8 3.1]         <- The element wise sum of the two
[2 3.1] + [1.8 2] = [3.8 5.1]
=======
[-0.2 1.1]
[1.8 3.1]                             <- The result I expect based on
[3.8 5.1]                                the above calculation
=======

What I get is

=======
[0 1.1]
[1 2.1]
[2 3.1]
=======
=======
[-0.2 0]
[0.8 1]
[1.8 2]
=======
[0 1.1] + [-0.2 0] = [-0.2 1.1]
[1 2.1] + [0.8 1] = [1.8 3.1]
[2 3.1] + [1.8 2] = [3.8 5.1]
=======
[4.6643e-310 4.6643e-310]
[4.94066e-323 6.95304e-310]           <- This is not the correct result
[6.94757e-310 4.6643e-310]
=======

So something in what vec_list operator+ returns is wrong and I would love to understand what I am doing wrong and why.

Comments