C # error: the call is ambiguous between the following methods or properties. Overload Operators

I have 2 classes with overloaded operators in the Dinero namespace, these are 2 classes:

First:

namespace Dinero { class Dollar { #region Atributos public Double cant; #endregion #region Constructores public Dollar() { this.cant = 0; } public Dollar(Double amount) { this.cant = amount; } #endregion #region Sobrecarga de Operadores public static Dollar operator +(Euro eu, Dollar dol) { Dollar devolucion = new Dollar(); devolucion.cant = eu.cant + (dol.cant * 1.3642); return devolucion; } public static Dollar operator -(Euro eu, Dollar dol) { Dollar devolucion = new Dollar(); devolucion.cant = eu.cant + (dol.cant * 1.3642); return devolucion; } public static bool operator ==(Euro eu, Dollar dol) { if (eu.cant == (dol.cant * 1.3642)) return true; else return false; } public static bool operator !=(Euro eu, Dollar dol) { if (eu.cant != (dol.cant * 1.3642)) return true; else return false; } #endregion } } 

Second:

 namespace Dinero { class Euro { #region Atributos public Double cant; #endregion #region Constructores public Euro() { this.cant = 0; } public Euro(Double amount) { this.cant = amount; } #endregion #region Sobrecarga de operadores public static Euro operator +(Euro eu, Dollar dol) { Euro devolucion = new Euro(); devolucion.cant = eu.cant + (dol.cant * 1.3642); return devolucion; } public static Euro operator -(Euro eu, Dollar dol) { Euro devolucion = new Euro(); devolucion.cant = eu.cant - (dol.cant * 1.3642); return devolucion; } public static bool operator ==(Euro eu, Dollar dol) { if (eu.cant == (dol.cant * 1.3642)) return true; else return false; } public static bool operator !=(Euro eu, Dollar dol) { if (eu.cant != (dol.cant * 1.3642)) return true; else return false; } #endregion } } 

And when I go to the main program (I don’t know how you guys call the main file, I would like to know, since I am full n00b), and I type this:

 namespace Ejercicio_21 { class Ejercicio_21 { static void Main(string[] args) { Console.Title = "Ejercicio Nro 21"; Euro euro00 = new Euro(1); Dollar dollar00 = new Dollar(1); Euro sumaEuros = euro00 + dollar00; 

On the last line, the compiler says:

Error 11 The call is ambiguous between the following methods or properties: 'Dinero.Euro.operator + (Dinero.Euro, Dinero.Dollar)' and 'Dinero.Dollar.operator + (Dinero.Euro, Dinero.Dollar)'

I guess this has something to do with different namespaces, but I couldn't figure it out even using Google.

This is the first question I ask here, so please don’t cry me into oblivion and please excuse my terrible English.

Note. I have to keep the Dollar and Euro classes in a different namespace other than the main program.

Thanks in advance.

+6
source share
3 answers

No, this has nothing to do with different namespaces - it is that you have the same operator signature declared in two places:

 public static Dollar operator +(Euro eu, Dollar dol) public static Euro operator +(Euro eu, Dollar dol) 

The compiler does not know which one you want to call.

Honestly, I don’t think that adding the dollar value to the value of the euro makes sense to start from the very beginning, but even after that, with the same operation “adding dollars to the euro” there should be one type of logical result.

If you really want the two operations were valid, I would suggest using the instance methods Plus :

 // In Dollar public Dollar Plus(Euro eu) // In Euro public Dollar Plus(Dollar dol) 

Then:

 Euro euro00 = new Euro(1); Dollar dollar00 = new Dollar(1); Euro sumaEuros = euro00.Plus(dollar00); 

Pretty clear, but without ambiguity.

Another alternative that I do not recommend is to make (say) that the first type of operand takes priority:

 public static Dollar operator +(Dollar dol, Euro eu) public static Euro operator +(Euro eu, Dollar dol) 

Then you could do:

 Dollar dol1 = ...; Euro eu1 = ...; Dollar dol2 = dol1 + eu1; Euro eu2 = eu1 + do1; 

This is pretty awful.

+10
source

This is due to the fact that you announced

 public static Your_Type operator +(Euro eu, Dollar dol) 

in both classes. Therefore, your compiler does not know which one is using ...

+1
source

The problem was solved in VS 2015 by deleting duplicate code that was both in the project and in the nuGet package. Unloading the project with duplicate code, the error disappeared.

0
source

Source: https://habr.com/ru/post/899561/


All Articles