EDIT: This is not a duplicate question. People are confusing my question as "why does my code not work" but its not that, its "why does it work on VS 2013 and not 2019" and that has been answered. This is not a duplicate question. I stated multiple times asking why it works in 2013 and not 2019.
So I was creating a DLL, and decided to upgrade from VS2013 to VS2019 so I would get the most recent visual studio packages and the most up to date IDE of course.
Out of nowhere a error became apparent after upgrading:
initial value of reference to non-const must be an lvalue
Since this error I have finally managed to fix it, but I would like to understand why this error happens, especially after upgrading to a different version of visual studio.
Simply put, the code that was working in visual studio 2013 was a vector structure with member functions and operator overloading as follows:
HEADER
struct Vec2 {
float x, y;
/* Insert other obligatory code */
Vec2 add(const Vec2);
Vec2 &operator+(const Vec2);
}
IMPLEMENTATION
Vec2 Vec2::add(const Vec2 o) {
return {
x + o.x,
y + o.y
};
}
Vec2 &Vec2::operator+(const Vec2 o) {
return add(o); // <-- 'initial value of reference to non-const must be an lvalue'
}
Like I said before, this code works and compiles perfectly. After I upgraded it was throwing the before mentioned error when it reached return add(o);
inside my overloaded plus operator function.
I just wanted to know why this started happening AFTER I upgraded... did something change in the MSVC compiler?
I did fix it though, this is my solution, but I am afraid that it might be inefficient...
NEW HEADER
struct Vec2 {
/* yada yada */
Vec2 &add(const Vec2);
Vec2 &operator+(const Vec2);
}
NEW IMPLEMENTATION
Vec2 &Vec2::add(const Vec2 o) {
Vec2 v(x + o.x, y + o.y); // Just normal constructor
return v;
}
Vec2 &Vec2::operator+(const Vec2 o) {
return add(o);
}
Any knowledge shared will be greatly appreciated, and if there is a better, faster, and/or easier solution to the one I implemented, please tell me! :)