I made a class "vec2f" and i was trying to overload its operands. According to what a read, binary operands should be non-members functions. The problem is that when I use the non-member operands it doesn't recongnize them.
Vec2f.h
You don't have a subtraction operator overload. You only overloaded the negation operator on line 23 (-x, not x - y).
The signature for subtraction is Vec2f Vec2f::operator-(const Vec2f &) const;
EDIT: Well, you did overload it in global scope, but you'll have an easier time if you move those overloads to the class scope. Just trust me on this.
I tried moving the overloads and it worked perfectly, like you said. The thing is, I read in several books that's good practice to define binary operators overloads in global scope. For that reason I'm insisting on this, but I can't find why my code isn't working.
C++ compilers are single pass except for classes. This means that they only see things that are declared further ahead when inside class scope. There's two solutions if you really want to keep the operators global:
1. Move the global declarations up:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//In header:
//Forward declare the class, or naming the class in the function declaration
//will generate compiler errors.
class Vec2f;
Vec2f operator+ (Vec2f v1, const Vec2f v2);
//etc.
class Vec2f{
//...
};
//In source:
Vec2f operator+ (Vec2f v1, const Vec2f v2)
{
return v1 += v2;
}
//etc.
2. Move the class declarations out: leave everything else the same, but move the definitions of lerp() and any other member function that uses those global operators to the source.