Avoiding ambiguities



 DEVELOP > c-Plus-Plus > Avoiding ambiguities

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "MiG"
Date: 01 Aug 2007 10:59:45 AM
Object: Avoiding ambiguities
Hello,
I have the following operators declared in a class:
// Provides R/W direct access to the matrix.
__forceinline const T& operator[](USHORT ndx) const throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }
__forceinline T& operator[](USHORT ndx) throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }
// Returns pointer to matrix data.
__forceinline operator const T*() const throw()
{ return(_mx); }
__forceinline operator T*() throw()
{ return(_mx); }
When I compile it I get the following errors:
error C2666: 'Matrix4<T>::operator []' : 4 overloads have similar
conversions
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1515): could
be 'float &Matrix4<T>::operator [](USHORT) throw()'
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1513): or
'const float &Matrix4<T>::operator [](USHORT) throw() const'
with
[
T=float
]
or 'built-in C++ operator[(const float *, int)'
or 'built-in C++ operator[(float *, int)'
Is there a way of avoiding these errors without having to create a
method for, say, replacing the functionality of the T* operators?
.

User: "Victor Bazarov"

Title: Re: Avoiding ambiguities 01 Aug 2007 12:22:53 PM
MiG wrote:

I have the following operators declared in a class:

// Provides R/W direct access to the matrix.
__forceinline const T& operator[](USHORT ndx) const throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }
__forceinline T& operator[](USHORT ndx) throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }

// Returns pointer to matrix data.
__forceinline operator const T*() const throw()
{ return(_mx); }
__forceinline operator T*() throw()
{ return(_mx); }

Please, when posting here, try to remove all compiler-specific
junk, like that ___blah nonsense. Your code is not easy to read
with all that crud in it.

When I compile it I get the following errors:

error C2666: 'Matrix4<T>::operator []' : 4 overloads have similar
conversions
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1515): could
be 'float &Matrix4<T>::operator [](USHORT) throw()'
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1513): or
'const float &Matrix4<T>::operator [](USHORT) throw() const'
with
[
T=float
]
or 'built-in C++ operator[(const float *, int)'
or 'built-in C++ operator[(float *, int)'


Is there a way of avoiding these errors without having to create a
method for, say, replacing the functionality of the T* operators?

Well, just don't provide the operator T*. Why do you think you need
it in the first place?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
User: "MiG"

Title: Re: Avoiding ambiguities 01 Aug 2007 01:06:22 PM
Victor Bazarov wrote:

MiG wrote:

I have the following operators declared in a class:

// Provides R/W direct access to the matrix.
__forceinline const T& operator[](USHORT ndx) const throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }
__forceinline T& operator[](USHORT ndx) throw()
{ _ASSERT(ndx < 16); return(_mx[ndx]); }

// Returns pointer to matrix data.
__forceinline operator const T*() const throw()
{ return(_mx); }
__forceinline operator T*() throw()
{ return(_mx); }


Please, when posting here, try to remove all compiler-specific
junk, like that ___blah nonsense. Your code is not easy to read
with all that crud in it.

When I compile it I get the following errors:

error C2666: 'Matrix4<T>::operator []' : 4 overloads have similar
conversions
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1515): could
be 'float &Matrix4<T>::operator [](USHORT) throw()'
with
[
T=float
]
d:\~alibrary\prj\cpp\xgl\0.1a1\inc\primitives.hpp(1513): or
'const float &Matrix4<T>::operator [](USHORT) throw() const'
with
[
T=float
]
or 'built-in C++ operator[(const float *, int)'
or 'built-in C++ operator[(float *, int)'


Is there a way of avoiding these errors without having to create a
method for, say, replacing the functionality of the T* operators?


Well, just don't provide the operator T*. Why do you think you need
it in the first place?

V

Alright V, thanks for that. I was really hoping there was a way to keep
the two operators. Not a problem though; I'll just remove the T*s...
Again, thanks V.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER