[Mono-list] Operator overloading

David Henderson dnadavewa at yahoo.com
Fri Jan 7 20:35:42 EST 2011


OK.  I have been trying to no avail to overload the > and < operators on a 
struct I wrote.  This is extremely easy in C++, but seems to not work in C#.

Here is my overload code:

    public static bool operator > ( lodData x , lodData y )
    {
      return x.getLOD() > y.getLOD();
    }
    public static bool operator < ( lodData x , lodData y )
    {
      return x.getLOD() < y.getLOD();
    }

getLOD returns a double.

Here is the error I see at runtime:

Unhandled Exception: System.InvalidOperationException: The comparer threw an 
exception. ---> System.ArgumentException: does not implement right interface
  at 
System.Collections.Generic.Comparer`1+DefaultComparer[permute_fdr+lodData].Compare
 (lodData x, lodData y) [0x00000]
  at System.Array.compare[lodData] (lodData value1, lodData value2, IComparer`1 
comparer) [0x00000]
  at System.Array.qsort[lodData,lodData] (.lodData[] keys, .lodData[] items, 
Int32 low0, Int32 high0, IComparer`1 comparer) [0x00000]
  at System.Array.Sort[lodData,lodData] (.lodData[] keys, .lodData[] items, 
Int32 index, Int32 length, IComparer`1 comparer) [0x00000]
  --- End of inner exception stack trace ---
  at System.Array.Sort[lodData,lodData] (.lodData[] keys, .lodData[] items, 
Int32 index, Int32 length, IComparer`1 comparer) [0x00000]
  at System.Array.Sort[lodData] (.lodData[] array, Int32 index, Int32 length, 
IComparer`1 comparer) [0x00000]
  at System.Collections.Generic.List`1[permute_fdr+lodData].Sort () [0x00000]
  at permute_fdr.fdr (System.Collections.Generic.List`1& pv, Double alpha, 
FDR_Types ftype) [0x00000]
  at permute_fdr.Main (System.String[] args) [0x00000]

I am assuming that the error manifests itself at a call to Sort() on a List of 
lodData items.

I have googled this and tried many iterations on the overload variation, none of 
which deviate from the error above.

Any help would be greatly appreciated.

Thanks!!

Dave H



      


More information about the Mono-list mailing list