PDA

View Full Version : GLM - compile error with the division



sajis997
02-04-2013, 02:46 PM
Hello forum,

I am getting with the following code snippet:




glm::vec2 points[NumPoints];

// Specifiy the vertices for a triangle
glm::vec2 vertices[3] =
{
glm::vec2( -1.0, -1.0 ), glm::vec2( 0.0, 1.0 ), glm::vec2( 1.0, -1.0 )
};

// Select an arbitrary initial point inside of the triangle
points[0] = glm::vec2( 0.25, 0.50 );

// compute and store N-1 new points
for ( int i = 1; i < NumPoints; ++i )
{
int j = rand() % 3; // pick a vertex at random

// Compute the point halfway between the selected vertex
// and the previous point
//GETTING COMILATION ERROR IN THE FOLLOWING LINE
points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
}




The error says there is matching operator.


Any idea to get around the problem?



Regards
Sajjad

trinitrotoluene
02-04-2013, 06:42 PM
Take care when you are defining a floating point number as a constant. 0.0 is a double by default for the compiler (i.e 64 bit) . Add an f after each of your floating point number, so the compiler know that you want a float (32bits), and your code should compile. Because glm::vec2 contain 2 numbers of type float, not double. The overloaded / operator with double is not defined for glm::vec2. As an advice, try to avoid mixing floating point data type when you are doing floating point arithmetic.

So replace at least



points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
with



points[i] = ( points[i - 1] + vertices[j] ) / 2.0f;