Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6692259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:50:51+00:00 2026-05-26T05:50:51+00:00

I have 2 classes with overloaded operators in a namespace called Dinero, these are

  • 0

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.

Thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:50:51+00:00Added an answer on May 26, 2026 at 5:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have classes of Type A and B: public class A { public
I have some classes layed out like this class A { public virtual void
I have two classes, and want to include a static instance of one class
I have the following classes: class Vertex { public: float X; float Y; float
I have classes which have automatic properties only like public customerName {get; set;}. They
I have a class IDocument which serve as a interface for some classes. It
Say I have three window classes, one for each OS I want to support:
Today I overloaded the << operator in one of my classes: #ifndef TERMINALLOG_HH #define
When using __declspec(dllexport) , should overloaded operators also have this exportation flag assigned? For
I have a class which has two overloaded functions. How do I export it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.