No operator "*" matches these operands

2.1k views Asked by At

this is my code

glm::vec3 v(4, -6, 7);

glm::vec3 twiceV = 2 * v;

I have included glm Stable and experimental glm extensions. Why I cannot use int * vec?

2

There are 2 answers

0
Wander Nauta On

2 is an integer, while the elements of a glm::vec3 are floats. Try this instead:

glm::vec3 twiceV = 2.0f * v;

I would also pass floating-point values to the constructor (4.0f), just to make it explicit that you're dealing with floats.

Alternatively, you can use an integer vector glm::ivec3:

glm::ivec3 v(4, -6, 7);
glm::ivec3 twiceV = 2 * v;

Of course, an integer vector will only hold integer values, which might not be what you want.

0
Bathsheba On

This is because there is no global overloaded operator of the form

glm::vec3 operator*(int, const glm::vec3&)

Does v * 2 work by any chance? (A member function operator overload would suffice for that.)

Or perhaps even 2f * v, which would then require the first parameter of the overloaded * operator to be a float?