C# 错误:以下方法或属性之间的调用不明确。重载运算符
我在名为 Dinero 的命名空间中有 2 个带有重载运算符的类,这些是 2 个类:
第一个:
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
}
}
第二个:
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
}
}
当我转到主程序时(我不知道你们如何调用主文件,我会我想知道,因为我是个 n00b ),我输入以下内容:
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;
关于最后一行,编译器说:
错误11以下方法之间的调用不明确或 属性: 'Dinero.Euro.operator +(Dinero.Euro, Dinero.Dollar)' 和 'Dinero.Dollar.operator +(Dinero.Euro, Dinero.Dollar)'
我认为它与不同的命名空间有关,但我无法弄清楚,即使使用谷歌也是如此。
这是我在这里问的第一个问题,所以请不要让我忘记,请原谅我糟糕的英语。
注意:我被迫将美元和欧元类保留在与主程序不同的命名空间中。
I have 2 classes with overloaded operators in a namespace called Dinero, these are the 2 classes:
First one:
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 one:
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'd like to know since I'm a total 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;
About 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 assume it has something to do with the different namespaces, but I couldn't figure it out, even using google.
This is the first question I ask here, so please, don't flame me to oblivion and please excuse my horrid English.
Note: I'm forced to keep Dollar and Euro classes in a different namespace than the main program.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过删除项目和 nuGet 包中的重复代码,在 VS 2015 中解决了此问题。通过卸载具有重复代码的项目,错误消失了。
Solved this issue in VS 2015 by deleting duplicated code that was in both a project as well as a nuGet package. By unloading the project that had the duplicated code, the error went away.
不,这与不同的命名空间无关 - 而是您在两个地方声明了相同的运算符签名:
编译器不知道您要调用哪一个。
老实说,我认为一开始将美元价值添加到欧元价值没有多大意义 - 但除此之外,具有相同的操作“将美元添加到欧元”应该具有单一的逻辑结果类型。
如果您确实想让这两个操作有效,我建议使用名为
Plus
的实例方法:Then:
非常清晰,但没有歧义。
我不推荐的另一种选择是让(比如说)第一个操作数的类型优先:
然后你可以这样做:
但这非常可怕。
No, it's got nothing to do with different namespaces - it's that you've got the same operator signature declared in two places:
The compiler doesn't know which one you want to call.
To be honest, I don't think adding a dollar value to a Euro value makes much sense to start with - but even beyond that, having the same operation "adding dollars to Euros" should have a single logical result type.
If you really want to make the two operations valid, I'd suggest having instance methods called
Plus
:Then:
Pretty much as clear, but without the ambiguity.
Another alternative which I don't recommend is to make (say) the first operand's type take priority:
Then you could do:
It's pretty horrible though.
这与您在两个班级中声明的事实有关
。
所以你的编译器不知道使用哪一个......
It has to do with the fact that you declared
in both classes.
So your compiler don't know which one use...