Matrix Operator() Overloading

Started by
2 comments, last by rip-off 13 years, 5 months ago
Hi, I am having problems with the compiler saying "expression must have (pointer-to-) function type" in the multiplication part of a Matrix.

Here are some snippets of the essential parts of the code.

class mat2{public:	/* Constructors and Deconstructors */	// No arguments gives an Identity Matrix	mat2() {		Identity(1.0f);	}	mat2(const float c) {		Identity(c);	}	mat2(const float c0, const float c1, 		 const float c2, const float c3) {			 m[0] = c0;			 m[1] = c1;			 m[2] = c2;			 m[3] = c3;	}	mat2(const mat2 &n) {		*this = n;	}	~mat2() {}	/* Array operator */	float &operator[] (int i);	const float &operator[] (int i) const;	const float &operator() (unsigned int i, unsigned int j) const;	float &operator() (unsigned int i, unsigned int j);	/* Arithmetic operators */	mat2 &operator=(const mat2 &n);	mat2 operator*(const mat2 &n);	mat2 operator*=(const mat2 &n);protected:	// This is arranged in rows format	// Each row contains one 2D Vector	enum { 		mat2lim = 4, row2lim = 2, col2lim = 2	};	float m[mat2lim];};


And here's the definitions of the class functions.

 inline float &mat2::operator[] (int i) {	return m;}inline const float &mat2::operator[] (int i) const {	return m;}/* Parenthesis operator */const float &mat2::operator() (unsigned int i, unsigned int j) const {	return m[(i * row2lim) + j];}float &mat2::operator() (unsigned int i, unsigned int j) {	// Row based matrix index	return m[(i * row2lim) + j];}/* Arithmetic operators */inline mat2 &mat2::operator=(const mat2 &n) {	for(int i = 0; i < mat2lim; i++) {		m = n;	}	return *this;}inline mat2 mat2::operator*(const mat2 &n) {	mat2 r;		r.m(0, 0) = m(0, 0) * n.m(0, 0) + m(0, 1) * n.m(1, 0);	// r[0][0]	r.m(0, 1) = m(0, 0) * n.m(0, 1) + m(0, 1) * n.m(1, 1);	// r[0][1]	r.m(1, 0) = m(1, 0) * n.m(0, 0) + m(1, 1) * n.m(1, 0),	// r[1][0]	r.m(1, 1) = m(1, 0) * n.m(0, 1) + m(1, 1) * n.m(1, 1);	// r[1][1]	return(r);}


The last part mat2::operator* is where the error occurs.
I am unsure what the problem is because there has been only one resource that has given me any clue of the overloading of the operator() for this purpose.
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.10

Can anyone point me in the right direction on fixing this matter?
Thanks
Advertisement
The member variable, m, is a one dimensional array of floats, overloading operator() has not changed this and so you cannot access elements from m using m(i,j) syntax, only m syntax.
Probably what you mean do be doing is invoking mat2::operator() on instances of mat2, for example:

r(0, 0) = (*this)(0, 0) * n(0, 0) + (*this)(0, 1) * n(1, 0); // r[0][0]
etc..
OMG!!!!! WOW! I spent the better half of the day doing all sorts of stupid stuff and all it was was to remove the .m's?!?!?

I could kiss you right now!
Thanks a lot!
It might be slightly more readable to rebind "this" as a reference:
const mat2 &self = *this;r(0, 0) = self(0, 0) * n(0, 0) + self(0, 1) * n(1, 0);	// r[0][0]

This topic is closed to new replies.

Advertisement