Sign in to follow this  
sforbes42

Visual Studio .NET 2003

Recommended Posts

Hi, I am getting a C2666 error when using an overloaded function in a class template I have written when I use a class pointer type as the data type for the template. Here is a portion of my template template <class DataType> class MArray { public: // Returns the item at the given index DataType& GetItem(int index); DataType& operator[](int index); }; Here is the class type class MState { private: // Some data public: // Some methods }; Here is the declaration and implementation: // Array of application states MArray<MState*> states(10); states[0] = NULL; If I call GetItem, everything is ok. If I used the overloaded operator[] I get the following error: d:\Home\Development\Games\Brew\mgamesdk\mstatemachine.cpp(34) : error C2666: 'MArray<DataType>::operator`[]'' : 2 overloads have similar conversions with [ DataType=MState * ] d:\Home\Development\Games\Brew\mgamesdk\ds\marray.h(54): could be 'MState &MArray<DataType>::operator [](const uint32) ' with [ DataType=MState * ] or 'built-in C++ operator[(MState ** , uint8)' while trying to match the argument list '(MStackA<DataType>, uint8)' with [ DataType=MState * ] Any ideas how I can solve this problem?

Share this post


Link to post
Share on other sites
Quote:
Original post by Eriond
Try adding const DataType& operator[](int index) const; might help :)


Thanks for the reply, I appreciate it. Unfortunately, that didn't work :( I haven't determined if this is a C++ problem or just a Visual Studio .NET problem.

Share this post


Link to post
Share on other sites
Here's what MSDN has to say about the issue: clicky

MSDN should be one of the first places to check if you get an error which you don't fully understand, for a lot of the compiler errors they will have examples on how to fix them and also some situations which causes them.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spudder
Here's what MSDN has to say about the issue: clicky

MSDN should be one of the first places to check if you get an error which you don't fully understand, for a lot of the compiler errors they will have examples on how to fix them and also some situations which causes them.


Thanks! I do often check there, but more often than not, I either do not find what I am looking for, it what I am looking for is buried so deep, it takes a while to find it. A Google search led me here.

That particular article covered the general overloading of functions. However, my problem seems to be with the operator[], where I want value[index] = something. I've already tried casting everything, and it still doesn't seem to work.

I've tried

(TYPE*)value[index] = something
value[index] = (TYPE*)something
(TYPE*)value[index] = (TYPE*)something

Nothing has resulted in removing the error. It may have to do with the fact I am using pointers and not literal type values. I've not found any examples of people trying to have a template with an overloaded [] operator, returning a pointer to a type, but I'll keep searching.

Oh, if I overload the () operator it works perfectly. Something to do with the [] it doesn't like.

Thanks Again.

Share this post


Link to post
Share on other sites
It's certainly possible, why don't you post the full code of your overloaded subscript operator?

Here's an example from my code.


// operator[] will only be enabled if the typex == typey == typez
template <typename X, typename Y, typename Z>
inline typename X &vec3<X, Y, Z>::operator[](size_t idx)
{
BOOST_STATIC_ASSERT((boost::mpl::and_<boost::is_same<X, Y>, boost::is_same<Y, Z> >::value));
assert(idx < 3);
return static_cast<X *>(&x)[idx];
}

template <typename X, typename Y, typename Z>
inline typename X const &vec3<X, Y, Z>::operator[](size_t idx) const
{
BOOST_STATIC_ASSERT((boost::mpl::and_<boost::is_same<X, Y>, boost::is_same<Y, Z> >::value));
assert(idx < 3);
return static_cast<X const*>(&x)[idx];
}


I have three different types because we deal with maps that are rather large and space is important. For instance, we may have geographic data that comes to us 4096km x 4096km, every pixel represents 100m, elevation values are floating point. So we would store as vec3<short, float, short>. That's irrelevant, just thought I'd answer before questions got posted leading this thread astray.

Share this post


Link to post
Share on other sites
You probably made your array class also have an overloaded implicit conversion to a pointer to the element type IE a member such as:

operator DataType*()


Which may cause ambiguity when using the subscript operator on your type.

Share this post


Link to post
Share on other sites
The problem is most likely that a literal "0" can mean a pointer NULL for any pointer, as well as an integer index, and even a float. You didn't post your entire class -- in fact, the case you posted, if compiled as-is, after fixing syntax errors, compiles just fine.

Chances are, you have more than one overload of operator[], and more than one of them accept something that the literal "0" can convert into.

Share this post


Link to post
Share on other sites

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