0

I know there are plenty of discussions about this topic, like this famous one (see sbi's answer). My goal is to overload the operator + for two vectors, starting from the operator +=, as explained in the linked famous question.

The following code snippet is quite classical

#include <iostream>
#include <cmath>

template <typename T>
class vector{
private:
    T* elem;
    std::size_t _size;
public:
    vector(const std::size_t size) : elem{new T[size]}, _size{size} {}
    ~vector(){ delete[] elem;}
    std::size_t size() const { return _size;}
    T& operator[](std::size_t i){ return elem[i];}
    const T& operator[](std::size_t i) const{ return elem[i];}
    vector& operator+=(const vector& b) ;
    vector& operator=(vector b);
};

template <typename T>
std::ostream& operator<<(std::ostream& os, const vector<T>& v) {
  for (auto i = 0u; i < v.size(); ++i)
    os << "v[" << i << "] = " << v[i] << std::endl;
  return os;
}

template <typename T>
vector<T>& vector<T>::operator+=(const vector<T>& b) {
    for (std::size_t i =0;i<_size;++i){
        elem[i]+=b[i];
    }
    return *this; 
}

template <typename T>
vector<T> operator+(vector<T> a,const vector<T>& b){
    a+=b;
    return a;
}



template <typename T>
vector<T>& vector<T>::operator=(vector<T> b){
    std::swap(*this,b);
    return *this;
}






int main(){


    vector<double> v{10};
    vector<double> w{10};
    for (auto i=0u;i<v.size();++i){
        v[i] = i;
        w[i]  = i*i;
    }

    v+=w;
    std::cout << v << std::endl;


    vector<double> v1{10};
    v1 = w + v;
    std::cout << v1 << std::endl;

    return 0;
}

After I compile, I obtain several warnings, and when I execute I obtain the following error

free(): double free detected in tcache 2 Aborted (core dumped)

and I can't undertand neither where it happens, nor how to fix it. I'm trying to stick as much as I can to the suggestions in the linked topic, but there's some bug in my overloading procedure, and I can't spot where/why.

Vefhug
  • 149
  • 5
  • 1
    You have a rule of three violation. You need a copy constructor as the default does a shallow copy, meaning two vectors will share the same buffer and one will delete it from the other. – NathanOliver Nov 06 '20 at 13:22
  • @NathanOliver Ups, I didn't face this topic yet. I'm trying to write this +operator without knowing what a shallow copy is – Vefhug Nov 06 '20 at 13:27
  • 1
    Well, you have to know how to properly copy objects, in order to implement the operator overload. This is not optional. It is mandatory. – Sam Varshavchik Nov 06 '20 at 13:29
  • Ok, so it's not possible to overload the "+" operator "(n my case for my vector class) without knowing how to copy objects. Could you just confirm that my +=operator is well overloaded? I mean, it just returns a reference, so it should be okay, right? @SamVarshavchik – Vefhug Nov 06 '20 at 13:31
  • That part is ok. However if `b`'s size is smaller than `this`'s size, this operator overload will obviously result in undefined behavior and garbage results. – Sam Varshavchik Nov 06 '20 at 13:33
  • Your `operator +=` is not correct. What happens if the vector you are adding to is bigger then the one you are adding from? – NathanOliver Nov 06 '20 at 13:33
  • Of course such an operation must be not defined. My idea was to throw an exception for the moment (it's the only thing about error handling that we saw), could it be okay with this adustment? @NathanOliver I'm sorry if this is too low level, but I've just began my c++ course and I was trying to go further with the vector class we saw at lecture – Vefhug Nov 06 '20 at 13:35
  • Could this be okay? @SamVarshavchik – Vefhug Nov 06 '20 at 13:47

0 Answers0