I've got a generic range class and I'm trying to add a comparison operator so I can test whether one range is equal to another. It fails to compile and I'm not sure how to fix the issues it's complaining about. Have I missed something obvious? Here's a snippet of the code:
generic<typename T>
public ref class Range
{
protected:
T m_min;
T m_max;
public:
...
...
bool operator==(Range<T>% rhs)
{
return ( m_min == rhs.m_min ) && ( m_max == rhs.m_max );
}
};
...which fails to compile with the following error:
1>c:\projects\Utils.h(47) : error C2676: binary '==' : 'T' does not define this operator or a conversion to a type acceptable to the predefined operator
Do I need to define conversions for each type that I want to overload (I'm using an Int32 instantiation)? I was hoping to avoid that sort of thing as it rather detracts from using generics.
[Edit] I've got an instantiation as follows:
Range<Int32> a = Range<Int32>(0,5);
Range<Int32> b = Range<Int32>(1,3);
if( Int32(2) != Int32(4) )
{
printf("Int32 supports != operator");
}
if( a != b )
{
printf("A != B : SUCCESS");
}
else
{
printf("A == B : FAIL");
}
...which compiles okay aside fromt he aforementioned errors. If I convert each value to an Int32 it compiles, but really I'd like to keep the class as generic as possible (i.e. not havnig to overload for each and every type). I guess I could subclass for each type and do the overloaded operators there, but the solution is less neat than I had expected when I first discovered generic
s ;-)