A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
Hi @Sid Kraft ,
Your function is passing all three vectors by value, so argvec4 is only a local copy inside the function. Because of that, the values assigned to out5 are not written back to Vec3 in the calling code.
I tested this and see that:
- when the third argument is passed by value, the caller’s vector remains unchanged
- when the third argument is passed by reference, the caller receives the computed result correctly
You should change the function signature to this:
void crossproductVector(const std::vector<float>& argvec2,
const std::vector<float>& argvec3,
std::vector<float>& argvec4)
The first two vectors are input only, so const std::vector<float>& is appropriate. The third vector is the output, so it must be a non-const reference.
For example:
void crossproductVector(const std::vector<float>& a,
const std::vector<float>& b,
std::vector<float>& c)
{
c.resize(3);
c[0] = a[1] * b[2] - a[2] * b[1];
c[1] = a[2] * b[0] - a[0] * b[2];
c[2] = a[0] * b[1] - a[1] * b[0];
}
If you saw an error such as “must be a modifiable lvalue” when using references, that usually means the parameter was declared as const std::vector<float>&. A const reference cannot be modified, so the output parameter must not be const.
Also, please double-check the formulas in your original code for the 2nd and 3rd components, because they do not match the standard cross-product formula. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guidance or provide feedback.
Thank you.