Skip to main content
added 464 characters in body
Source Link
evanmcdonnal
  • 48.5k
  • 17
  • 111
  • 119

It's because this;this

MVec MVec::operator+(const MVec &vec1) const { MVec ans; for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; } 

is defined to take only one argument.

It needs toshould be defined as a non-member function with the this MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const definition. then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

Also, you should add bounds checking. If either vector's length is less than three you're going to crash. You should either add up to the length of the shorter vector or not add them at all of the lengths aren't the same. example

 int loopVar = 0; if (vec1.length() > vec2.length()) loopVar = vec2.length(); else loopVar = vec1.length(); for (int i = 0; i < loopVar; i++) ans[i] = vec1[i] + vec2[i]; 

It's because this;

MVec MVec::operator+(const MVec &vec1) const { MVec ans; for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; } 

is defined to take only one argument.

It needs to be MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

this

MVec MVec::operator+(const MVec &vec1) const { MVec ans; for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; } 

should be defined as a non-member function with the this MVec operator+(const MVec &vec1, const MVec &vec2) const definition. then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

Also, you should add bounds checking. If either vector's length is less than three you're going to crash. You should either add up to the length of the shorter vector or not add them at all of the lengths aren't the same. example

 int loopVar = 0; if (vec1.length() > vec2.length()) loopVar = vec2.length(); else loopVar = vec1.length(); for (int i = 0; i < loopVar; i++) ans[i] = vec1[i] + vec2[i]; 
added 2 characters in body
Source Link
Lightness Races in Orbit
  • 386.8k
  • 77
  • 670
  • 1.1k

It's because this;

MVec MVec::operator+(const MVec &vec1) const { MVec ans;

MVec MVec::operator+(const MVec &vec1) const {  MVec ans;  for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; } 

}

is defined to take only one argument.

It needs to be MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

It's because this;

MVec MVec::operator+(const MVec &vec1) const { MVec ans;

 for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; 

}

is defined to take only one argument.

It needs to be MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

It's because this;

MVec MVec::operator+(const MVec &vec1) const {  MVec ans;  for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; } 

is defined to take only one argument.

It needs to be MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.

Source Link
evanmcdonnal
  • 48.5k
  • 17
  • 111
  • 119

It's because this;

MVec MVec::operator+(const MVec &vec1) const { MVec ans;

 for(int i = 0; i < 3; i++) ans[i] = vec[i] + vec1[i]; return ans; 

}

is defined to take only one argument.

It needs to be MVec MVec::operator+(const MVec &vec1, const MVec &vec2) const then you need to modify to add vec1[i] + vec2[i], assuming your goal is to add every value in the two vectors.