Vector addition using class and operator overload

Hello, I'm trying to implement addition of two multidimensional vectors, i.e (M x 2), using operator overload, which is very similar to the example given in the C++ website.

When i run the code, i get an error in a pop up window- "Debug Assertion failed, Program: C:\...|vector, Line 756, expression: vector subscript out of range, abort,retry or ignore"

// overloading the + operator

Cadd Cadd::operator + (Cadd param) {
Cadd temp;
int i;
for (i=0; i<=sizeof(u); i++)
temp.u[i] = u[i] + param.u[i];
temp.v[i] = v[i] + param.v[i];
return (temp);
}

i
Last edited on
You do not use "sizeof" to get the size of the vector. Use vector::size() instead. Also, you can use vector::at() to get an exception when going out of range.
Topic archived. No new replies allowed.