operator [] overloading error 
Author Message
 operator [] overloading error

An interesting error is generated when I overload the Array element operator
with a USHORT index. When I attempt to use the operator, error C2666 tells
me that 3 overloads have similar conversions. If I change the USHORT
declaration to an int, it compiles fine.

ie:

class A {
protected:
    object *pObjects;
public:
    // If I change nIndex declaration from USHORT to int here, the error
disappears.
    object* operator[](USHORT nIndex) { return pObject + nIndex; }

Quote:
};

void main() {
    A a;
    object *pObject;
    ...
    pObject = a[0];        // Gives the error 2666

Quote:
};

Mick


Sat, 21 May 2005 11:14:18 GMT  
 operator [] overloading error

Quote:
> An interesting error is generated when I overload the Array element
operator
> with a USHORT index. When I attempt to use the operator, error C2666 tells
> me that 3 overloads have similar conversions. If I change the USHORT
> declaration to an int, it compiles fine.

> ie:

> class A {
> protected:
>     object *pObjects;
> public:
>     // If I change nIndex declaration from USHORT to int here, the error
> disappears.
>     object* operator[](USHORT nIndex) { return pObject + nIndex; }
> };

> void main() {
>     A a;
>     object *pObject;
>     ...
>     pObject = a[0];        // Gives the error 2666

The type of the literal '0' is 'int', so an operator[] which takes an int as
the index parameter is an exact match, while one that takes USHORT requires
(at least one) conversion.

Quote:
> };

> Mick

-cd


Sat, 21 May 2005 12:21:18 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. error C2440 when try to compile template class with overloaded operator

2. CXX0058: Error: Overloaded Operator Not Found

3. error C2440 when try to compile template class with overloaded operator

4. error c2440 returned trying to compile overloaded [] operator

5. VC++ 5.0 ambiguity: conversion operator vs overloaded operator?

6. == and != operator (operator overloading)

7. Using C# operator overloading in VB.NET

8. Can't overload operator new in VC7.

9. HELP: Overloading Assignment Operators in MC++

10. Overloading Assignment Operators in MC++

11. stupid operator overloading syntax

12. Wrapping unmanaged operator overloads?

 

 
Powered by phpBB® Forum Software