Problem with override the operators
-
I'm trying to override those operators:
==, !=, <, >, <=, >=, <<, >>
...but VS10 give me this error:
Overloadable unary operator expected
Is there some special conditions to make this work..? Cause those operators are overloadable...
-
-
I'm trying to override those operators:
==, !=, <, >, <=, >=, <<, >>
...but VS10 give me this error:
Overloadable unary operator expected
Is there some special conditions to make this work..? Cause those operators are overloadable...
And we'd need to see your code where you're overloading these operators to tell you what you're doing wrong.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I'm trying to override those operators:
==, !=, <, >, <=, >=, <<, >>
...but VS10 give me this error:
Overloadable unary operator expected
Is there some special conditions to make this work..? Cause those operators are overloadable...
-
I'm trying to override those operators:
==, !=, <, >, <=, >=, <<, >>
...but VS10 give me this error:
Overloadable unary operator expected
Is there some special conditions to make this work..? Cause those operators are overloadable...
As Eddy states, those aren't Unary operators. This would indicate that these aren't the ones that are causing you problems. Could you post a sample that shows the code that the compiler is complaining about?
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier -
As Eddy states, those aren't Unary operators. This would indicate that these aren't the ones that are causing you problems. Could you post a sample that shows the code that the compiler is complaining about?
I was brought up to respect my elders. I don't respect many people nowadays.
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easierOK, thanks guys for the links, but I've already seen this. Problem is that I'm trying to convert simple class from C++ to CS. Here is the code:
class SOME_API SomeClass
{
public:
// Accessors.
bool operator==( const SomeClass& Other ) const
{
return Index == Other.Index;
}bool operator!=( const SomeClass& Other ) const { return Index != Other.Index; } //...
private:
// SomeClass index.
int Index;//...
};
Is there any chance to convert this to CS without writing a bunch of code?
-
OK, thanks guys for the links, but I've already seen this. Problem is that I'm trying to convert simple class from C++ to CS. Here is the code:
class SOME_API SomeClass
{
public:
// Accessors.
bool operator==( const SomeClass& Other ) const
{
return Index == Other.Index;
}bool operator!=( const SomeClass& Other ) const { return Index != Other.Index; } //...
private:
// SomeClass index.
int Index;//...
};
Is there any chance to convert this to CS without writing a bunch of code?
A rough untested example of how I do this for a
class
(it's much simpler for astruct
as it can never benull
):// An immutable int wrapper class
public class SomeClass : IEquatable<SomeClass>
{
private int index;public SomeClass(int index) { this.index = index; } public static bool operator ==(SomeClass instance, SomeClass other) { if(object.ReferenceEquals(instance, other)) return true; if(object.ReferenceEquals(null, instance) || object.ReferenceEquals(null, other)) return false; return instance.index == other.index; } public static bool operator !=(SomeClass instance, SomeClass other) { return !(instance == other); } public int Index { get { return index; } } public override bool Equals(object obj) { return Equals(obj as SomeClass); } public bool Equals(SomeClass other) { if(object.ReferenceEquals(null, other)) return false; return index.Equals(other.index); } public override int GetHashCode() { return index; }
}
And a struct:
// An immutable int wrapper struct
public struct SomeStruct : IEquatable<SomeStruct>
{
private int index;public SomeStruct(int index) { this.index = index; } public static bool operator ==(SomeStruct instance, SomeStruct other) { return instance.index == other.index; } public static bool operator !=(SomeStruct instance, SomeStruct other) { return !(instance == other); } public int Index { get { return index; } } public override bool Equals(object obj) { if(obj is SomeStruct) return Equals((SomeStruct)obj); return false; } public bool Equals(SomeStruct other) { return index.Equals(other.index); } public override int GetHashCode() { return index; }
}
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pra -
I'm trying to override those operators:
==, !=, <, >, <=, >=, <<, >>
...but VS10 give me this error:
Overloadable unary operator expected
Is there some special conditions to make this work..? Cause those operators are overloadable...
Unary operators require one parameter, so it's assuming that the operator should be a unary one as you are only supplying one parameter. The operators you have listed are binary operators, so require two parameters. See my example in my other post and my article An Introduction to Operator Overloading in C#[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Unary operators require one parameter, so it's assuming that the operator should be a unary one as you are only supplying one parameter. The operators you have listed are binary operators, so require two parameters. See my example in my other post and my article An Introduction to Operator Overloading in C#[^]
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
Thanks for the snippets DaveyM69 - I've ended up with something similar. For now, to compare I'm using GetIndex(), but my converted code is constantly growing. Didn't know that CS is so much more complicated... Thanks again...
I don't believe C# is much more complicated with this; I think the real difference is that you'd typically let the overloaded operators be member functions in C++ and C# wants them to be static functions. So if you rewrite your C++ code to the static overloading, then you're already having the same code in C# and C++.