C# 错误:以下方法或属性之间的调用不明确。重载运算符

发布于 2024-12-10 22:05:02 字数 3316 浏览 0 评论 0原文

我在名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

乖乖公主 2024-12-17 22:05:03

通过删除项目和 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.

感情洁癖 2024-12-17 22:05:02

不,这与不同的命名空间无关 - 而是您在两个地方声明了相同的运算符签名:

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

编译器不知道您要调用哪一个。

老实说,我认为一开始将美元价值添加到欧元价值没有多大意义 - 但除此之外,具有相同的操作“将美元添加到欧元”应该具有单一的逻辑结果类型。

如果您确实想让这两个操作有效,我建议使用名为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);

非常清晰,但没有歧义。

我不推荐的另一种选择是让(比如说)第一个操作数的类型优先:

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

然后你可以这样做:

Dollar dol1 = ...;
Euro eu1 = ...;

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

但这非常可怕。

No, it's got nothing to do with different namespaces - it's that you've got 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 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:

// 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 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:

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;

It's pretty horrible though.

傲鸠 2024-12-17 22:05:02

这与您在两个班级中声明的事实有关

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


所以你的编译器不知道使用哪一个......

It has to do with the fact that you declared

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

in both classes.
So your compiler don't know which one use...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文