#### Archived

This topic is now archived and is closed to further replies.

# Operator ambiguity

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

## Recommended Posts

hi guys, this concerns an issue with operator overloading. in my vector class, i tried setting overloaded operators for the array index and for casting. however, i cant seem to have both at the same time. sample:

const float operator [] ( const long &index ) const;
operator float*() const;

now the error i get says i have 2 ambiguous definitions for the [] operator. it seems to make sense since the array index operator is usually interpreted as a pointer too. but the difference between the operators lies in that one takes a parameter and the other doesn't. yet the code won't compile. if i leave only one and not the other (doesnt matter which one) then it works. i use MSVC6. so it could be a compiler problem. i checked the msdn for the reported error and the example case provided does not coincide with mine entirely. help is appreciated. thanx. [edited by - crazee on April 21, 2003 1:07:50 PM]

##### Share on other sites
Yeah, the problem is that the compiler has the choice of using your subscript operator, or implicitly converting to float* and then using the built-in subscript operator. This is why typecast operators are generally a really, really bad idea.

What are you trying to do here? Why do you want to convert to a float*?

How appropriate. You fight like a cow.

##### Share on other sites
Don''t provide an implicit operator to float. If you want an easy way to call glVertex3fv with your vector class, you should make an overloaded version:

  void glVertex3fv(MyVector3& v){  glVertex3fv(&v[0]);}

You can do the same for whatever else you needed to cast to float* for.

##### Share on other sites
sneftel: the reason i''m using explicit cast operator is coz i''m using it in a vector math class. the vector parameters are defined as a union of 4 float parameters, public too. now it is already convenient to access the values as x,y,z OR v[0...2].

i just wanted to overload the index operator[] in case i REALLY get lazee but the cast operator comes in handy in case i want to directly cast the vector into an array of floats:

without my intended approach:

float *list = vectorTemp.v; OR
float *list = &vectorTemp.x;

with my intended approach:

float *list = (float*)vectorTemp;

it felt simpler for me to work with the casting way, though i think it is slower. anyway, it was also because i wanted to learn a bit about operator overloading. guess i got carried away. i scrapped the casting operator though.. found the index operator more useful.

##### Share on other sites
cgoat: ur suggestion doesn''t work coz the issue doesnt seem to be with return type. in fact wat boggled me initially is y does ambiguity even exist when the parameter list for the operators differ.

using the operator[] REQUIRES an index integral, but the cast doesnt need. so y does msvc go through all the trouble of trying to do implicit casting before indexing instead of simply using my self-made operator. kinda irritating when the compiler stops listening to you.. grrr

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633768
• Total Posts
3013753
×