Invalid User Defined Conversion

Hey All. Working through a course on MIT OCW and starting the 2nd assignment (Assignment1) located here:
http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-837-computer-graphics-fall-2012/assignments/

The assignment provides a fairly large set of skeletal code dealing with openGL coordinate transformations. There are a lot of matrix multiplications that will be happening and so the instructor has developed a class Matrix4f which is giving me a lot of problems. The class is effectively an array of 16 floats.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Matrix4f::Matrix4f( float m00, float m01, float m02, float m03,
				   float m10, float m11, float m12, float m13,
				   float m20, float m21, float m22, float m23,
				   float m30, float m31, float m32, float m33 )
{
	m_elements[ 0 ] = m00;
	m_elements[ 1 ] = m10;
	m_elements[ 2 ] = m20;
	m_elements[ 3 ] = m30;
	
	m_elements[ 4 ] = m01;
	m_elements[ 5 ] = m11;
	m_elements[ 6 ] = m21;
	m_elements[ 7 ] = m31;

	m_elements[ 8 ] = m02;
	m_elements[ 9 ] = m12;
	m_elements[ 10 ] = m22;
	m_elements[ 11 ] = m32;

	m_elements[ 12 ] = m03;
	m_elements[ 13 ] = m13;
	m_elements[ 14 ] = m23;
	m_elements[ 15 ] = m33;
}


The error is being thrown in several locations, but likely for the same reason. Here is an example location:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef EXTRA_H
#define EXTRA_H

#ifdef WIN32
#include <windows.h>
#endif
#include <GL/gl.h>

#ifndef M_PI
#define M_PI  3.14159265358979
#endif

// Inline functions to help with drawing
inline void glVertex( const Vector3f& a )
{
    glVertex3fv(a);
}

inline void glNormal( const Vector3f& a ) 
{
    glNormal3fv(a);
}

inline void glLoadMatrix( const Matrix4f& m )
{
    glLoadMatrixf( m ); <
}

inline void glMultMatrix( const Matrix4f& m )
{
    glMultMatrixf( m ); <<---- Here
}

#endif


2 distinct errors occur:

In file included from D:\Coding\CG\Assignment1\camera.cpp:2:0:
D:\Coding\CG\Assignment1\extra.h: In function 'void glLoadMatrix(const Matrix4f&)':
D:\Coding\CG\Assignment1\extra.h:26:22: error: invalid user-defined conversion from 'const Matrix4f' to 'const GLfloat* {aka const float*}' [-fpermissive]
In file included from D:\Coding\CG\Assignment1/vecmath.h:6:0,
from D:\Coding\CG\Assignment1\camera.h:9,
from D:\Coding\CG\Assignment1\camera.cpp:1:
D:\Coding\CG\Assignment1/Matrix4f.h:65:2: note: candidate is: Matrix4f::operator float*() <near match>
D:\Coding\CG\Assignment1/Matrix4f.h:65:2: note: no known conversion for implicit 'this' parameter from 'const Matrix4f*' to 'Matrix4f*'
In file included from D:\Coding\CG\Assignment1\camera.cpp:2:0:
D:\Coding\CG\Assignment1\extra.h:26:22: error: passing 'const Matrix4f' as 'this' argument of 'Matrix4f::operator float*()' discards qualifiers [-fpermissive]


I'm looking for help troubleshooting these errors. If it is helpful, you can download/view/compile all of the code using the above link. Note that Assignment1 uses some homemade classes from Assignment0. So if you wanted to compile/view the code, you'd have to take ALL of the files from Assignment1, but also add in some of the .h/.cpp files included in Assignment0.
Last edited on
Pass the internal array to the functions

glLoadMatrixf( m.m_elements );


> candidate is: Matrix4f::operator float*() <near match>
> error: passing 'const Matrix4f' as 'this' argument of 'Matrix4f::operator float*()' discards qualifiers [-fpermissive]
The casting member function is missing a const version
1
2
3
Matrix4f::operator const float*() const{
   return m_elements;
}
Last edited on
The Matrix4f class includes a conversion operator for const float*, which is what is required here. Is your version missing it?

1
2
3
	// ---- Utility ----
	operator float* (); // automatic type conversion for GL
	operator const float* () const; //   <- This is the convesion that will match. 
It appears so... Mine looks like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#ifndef MATRIX4F_H
#define MATRIX4F_H

#include <cstdio>

class Matrix2f;
class Matrix3f;
class Quat4f;
class Vector3f;
class Vector4f;

// 4x4 Matrix, stored in column major order (OpenGL style)
class Matrix4f
{
public:

    // Fill a 4x4 matrix with "fill".  Default to 0.
	Matrix4f( float fill = 0.f );
	Matrix4f( float m00, float m01, float m02, float m03,
		float m10, float m11, float m12, float m13,
		float m20, float m21, float m22, float m23,
		float m30, float m31, float m32, float m33 );
	
	// setColumns = true ==> sets the columns of the matrix to be [v0 v1 v2 v3]
	// otherwise, sets the rows
	Matrix4f( const Vector4f& v0, const Vector4f& v1, const Vector4f& v2, const Vector4f& v3, bool setColumns = true );
	
	Matrix4f( const Matrix4f& rm ); // copy constructor
	Matrix4f& operator = ( const Matrix4f& rm ); // assignment operator
	// no destructor necessary

	const float& operator () ( int i, int j ) const;
	float& operator () ( int i, int j );

	Vector4f getRow( int i ) const;
	void setRow( int i, const Vector4f& v );

	// get column j (mod 4)
	Vector4f getCol( int j ) const;
	void setCol( int j, const Vector4f& v );

	// gets the 2x2 submatrix of this matrix to m
	// starting with upper left corner at (i0, j0)
	Matrix2f getSubmatrix2x2( int i0, int j0 ) const;

	// gets the 3x3 submatrix of this matrix to m
	// starting with upper left corner at (i0, j0)
	Matrix3f getSubmatrix3x3( int i0, int j0 ) const;

	// sets a 2x2 submatrix of this matrix to m
	// starting with upper left corner at (i0, j0)
	void setSubmatrix2x2( int i0, int j0, const Matrix2f& m );

	// sets a 3x3 submatrix of this matrix to m
	// starting with upper left corner at (i0, j0)
	void setSubmatrix3x3( int i0, int j0, const Matrix3f& m );

	float determinant() const;
	Matrix4f inverse( bool* pbIsSingular = NULL, float epsilon = 0.f ) const;

	void transpose();
	Matrix4f transposed() const;

	// ---- Utility ----
	operator float* (); // automatic type conversion for GL
	void print();

	static Matrix4f ones();
	static Matrix4f identity();
	static Matrix4f translation( float x, float y, float z );
	static Matrix4f translation( const Vector3f& rTranslation );
	static Matrix4f rotateX( float radians );
	static Matrix4f rotateY( float radians );
	static Matrix4f rotateZ( float radians );
	static Matrix4f rotation( const Vector3f& rDirection, float radians );
	static Matrix4f scaling( float sx, float sy, float sz );
	static Matrix4f uniformScaling( float s );
	static Matrix4f lookAt( const Vector3f& eye, const Vector3f& center, const Vector3f& up );
	static Matrix4f orthographicProjection( float width, float height, float zNear, float zFar, bool directX );
	static Matrix4f orthographicProjection( float left, float right, float bottom, float top, float zNear, float zFar, bool directX );
	static Matrix4f perspectiveProjection( float fLeft, float fRight, float fBottom, float fTop, float fZNear, float fZFar, bool directX );
	static Matrix4f perspectiveProjection( float fovYRadians, float aspect, float zNear, float zFar, bool directX );
	static Matrix4f infinitePerspectiveProjection( float fLeft, float fRight, float fBottom, float fTop, float fZNear, bool directX );

	// Returns the rotation matrix represented by a quaternion
	// uses a normalized version of q
	static Matrix4f rotation( const Quat4f& q );

	// returns an orthogonal matrix that's a uniformly distributed rotation
	// given u[i] is a uniformly distributed random number in [0,1]
	static Matrix4f randomRotation( float u0, float u1, float u2 );

private:

	float m_elements[ 16 ];

};

// Matrix-Vector multiplication
// 4x4 * 4x1 ==> 4x1
Vector4f operator * ( const Matrix4f& m, const Vector4f& v );

// Matrix-Matrix multiplication
Matrix4f operator * ( const Matrix4f& x, const Matrix4f& y );

#endif // MATRIX4F_H


If so, that's kind of frustrating. Is vector/Matrix 2f/3f/4f a set of well known classes that is use when doing openGL? If so, where should I be getting the most up-to-date source for these. The OCW class appears not to have given them to me :/.
If so, that's kind of frustrating. Is vector/Matrix 2f/3f/4f a set of well known classes that is use when doing openGL?

No. Although I'm sure implementing them isn't uncommon.

The "Starter code for Microsoft Visual Studio 2010" included a version of vecmath.h with the appropriate conversion defined.
Ahh thank you very much. I wasn't using Visual Studio, so I didn't even think to look there.
Topic archived. No new replies allowed.