Sign in to follow this  

Overloading operator [] woes.

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a Matrix class right now and I'm trying to allow code like float _11 = myMatrix[1][1]; to work. I keep getting some wierd ambiguous error saying how the compiler can't decide between my classes operator [] or the built in operator [] Here is my error
c:\MatrixTest.cpp(96) : error C2666: 'math::Matrix::operator`[]'' : 4 overloads have similar conversions
c:\Matrix.h(118): could be 'const float *math::Matrix::operator [](unsigned int) const'
c:\Matrix.h(117): or       'float *math::Matrix::operator [](unsigned int)'
  or       'built-in C++ operator[(float *, int)'
  or       'built-in C++ operator[(const float *, int)'
  while trying to match the argument list '(math::Matrix, int)'

Here is my function and the line the error occured on.
inline const float* Matrix::operator []( unsigned int index ) const
{
	// Pre condition.
	assert( index >= 0 && index <= 3 );
	return m_indices[index];	
}

inline float*	Matrix::operator []( unsigned int index )
{
	// Pre condition.
	assert( index >= 0 && index <= 3 );
	return m_indices[index];
} 

// ERROR OCCURS ON THIS LINE
result = static_cast<double>(testMatrix[0][2]);


Share this post


Link to post
Share on other sites
Don't return pointers (un-less it was a pointer to an array which is different) to one of the dimensions of multi-dimensional arrays this leads to pointer degradation and lost of all the good stuff that static arrays have, beside the type of a one of the dimensions of multi-dimensional arrays is a (const) reference to an (multi) array, change it to this:


#include <cstddef>

template < typename T, size_t M, size_t N = M >
class matrix {

T f[M][N];

public:

typedef T (&array_ref)[N];
typedef T const (&const_array_ref)[N];

const_array_ref operator[](size_t i) const {
return f[i];
}

array_ref operator[](size_t i) {
return f[i];
}
};

#include <iostream>

int main() {

typedef matrix<float, 4> matrix4f;
matrix4f m;

m[0][0] = 30;

std::cout << m[0][0] << std::endl;

}


[Edited by - snk_kid on February 13, 2005 4:54:36 AM]

Share this post


Link to post
Share on other sites
Quote:
Original post by ph33r
I'm writing a Matrix class right now and I'm trying to allow code like float _11 = myMatrix[1][1]; to work. I keep getting some wierd ambiguous error saying how the compiler can't decide between my classes operator [] or the built in operator []

c:\MatrixTest.cpp(96) : error C2666: 'math::Matrix::operator`[]' : 4 overloads have similar conversions
c:\Matrix.h(118): could be 'const float *math::Matrix::operator [](unsigned int) const'
c:\Matrix.h(117): or 'float *math::Matrix::operator [](unsigned int)'
or 'built-in C++ operator[(float *, int)'
or 'built-in C++ operator[(const float *, int)'
while trying to match the argument list '(math::Matrix, int)'

Looks like you made your class have implicit conversion to float*. Get rid of that.

I.E. Get rid of


operator float*()

//and

operator float const*() const


in your matrix class as they create ambiguity with your overloaded indexing operators (the compiler doesn't know if it should use your index operator or if it should implicitly convert to float* and then use the built-in index operators for pointers).

As well, prefer to use ::std::size_t (include cstddef) instead of unsigned ints, and as snk said, if your sizes are known at compile time, prefer returning array references (or, even better, a type which encapsulates one, if you have the time).

Share this post


Link to post
Share on other sites

This topic is 4684 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this