0

I created my own vector template class and overloaded multiple operators. But if i run my code i get acces violation at the templates [] operator. It seems like the + operation is success but it returns something wrong, as in the Vector3 constructor with the template vector as argument it cant access the elements. What am i doing wrong?

int main(int argc, char* args[])
{

    Vector3 vec(1, 2, 3);
    Vector3 vec2(3, 2, 1);

    Vector3 vec3 = vec + vec2;

    std::cout << vec3 << std::endl;
}

Vector.h

template <typename T, size_t N>
class Vector {
protected:
    T* elems;

public:
    Vector() {
        elems = new T[N];
    }

    Vector(Vector<T, N> const& r) {
        elems = new T[N];
        for (int i = 0; i < N; i++) {
            elems[i] = r[i];
        }
    }

    T operator[](size_t i) const { return elems[i]; }
    T& operator[](size_t i) { return elems[i]; }

    Vector<T,N>& operator+(Vector<T,N> const& other) {
        Vector<T,N> res;
        for (int i = 0; i < N; i++) {
            res.elems[i] = elems[i] + other.elems[i];
        }
        return res;
    }

friend std::ostream& operator<<(std::ostream& os, const Vector& vec) {
        os << "(";
        for (int i = 0; i < N; i++) {
            os << vec[i];
            if (i != N - 1) {
                os << ",";
            }
        }
        os << ")";

        return os;
    }
};

Vector3.h

class Vector3 : public Vector<GLfloat, 3>{
public:
    Vector3() {
        elems[0] = 0;
        elems[1] = 0;
        elems[2] = 0;
    }

    Vector3(GLfloat x, GLfloat y, GLfloat z) {
        elems[0] = x;
        elems[1] = y;
        elems[2] = z;
    }

    Vector3(GLfloat x, GLfloat y) {
        elems[0] = x;
        elems[1] = y;
        elems[2] = 0;
    }

    Vector3(const Vector<GLfloat, 3> &vec) {
        elems[0] = vec[0];
        elems[1] = vec[1];
        elems[2] = vec[2];
    }}
Marko Taht
  • 1,448
  • 1
  • 20
  • 40
  • TL;DR of the dupe: You are returning a reference to a local variable in your `operator +`. The reference is a dangling reference as all non-static function local variables are destroyed when the function ends. Change the function to have the signature of `Vector operator+(Vector const& other)` so you return by value instead. Also, you need to define a copy assignment operator and a destructor for you class or you're going to get memory leaks. See this for more information: https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three – NathanOliver Jun 03 '20 at 13:08
  • @NathanOliver so if i would declare res as a pointer and return the pointer then it would be fine ? – Marko Taht Jun 03 '20 at 13:13
  • 2
    Iit depends on the actual code but a pointer is not needed. Just change the return type of the function from `Vector&` to `Vector` and you don't need to change anything else. – NathanOliver Jun 03 '20 at 13:16
  • If you had turned all the compiler warnings on, the compiler would have pointed out the mistake for you. – Eljay Jun 03 '20 at 13:26

0 Answers0