Thread: No Match For Operator+ ???????

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    Well, let's give him a hint! A very straightforward way, you will find this in basic text books, is to overload operators as class member functions:

    Code:
    #include <iostream>
    #include <string>
    
    class Integer
    {
      public:
        Integer(int i);
        Integer(std::string str);
    
        int getValue();
        void setValue(int i);
    
        Integer& operator+( Integer const& arg); // why use 'Integer' AND 'int'
    
      private:
        int myValue;
    };
    
    // ***
    
    Integer& Integer :: operator+( Integer const& arg)
    {
        this->myValue += arg.myValue;
        return *this;
    }
    However, try to minimize your class with functions that concern your (private) attributes in the most direct way only, e.g., the constructors (default, copy, cast, ..) 'setValue', 'getValue'. All other functions are best no-member, no-friend.

    Why? Try to overload the operator+ for a class Double:

    Code:
    #include <iostream>
    #include <string>
    
    class Double
    {
      public:
        Double(double i);
        Double(std::string str);
    
        double getValue();
        void setValue(double i);
    
        Double& operator+( Double const& arg); // suppose you have defined a class 'Double'
        Double& operator+( Integer const& arg); // to add your Integer to your Double
    
      private:
        double myValue;
    };
    
    // ***
    
    Integer& Integer :: operator+( Integer const& arg)
    {
        this->myValue += arg.myValue;
        return *this;
    }
    
    Double& Double:: operator+( Integer const& arg)
    {
        this->myValue+= arg.myValue;
        return *this;
    }
    This will work for ' double + int' , but it won't for ' int + double ' . Why? In the member-operator, '*this', so the double, is the left (implicit) argument of '+', and 'int' the right argument. You cannot invert these.
    Now you can say: "Ok, but I simply write an 'operator+( Double)' in my class 'Integer' ". Wrong: you cannot declare this before the class 'Double' has been declared!

    An even stronger argument: if you want to add an object of a class your colleague wrote, to your Integer, you cannot modify his/her class. And you do want this operator to be commutative.

    There are better arguments, but let's hold for now.

    I would try it like this:

    Code:
    #include <iostream>
    #include <string>
    
    class Integer
    {
      public:
        Integer(); // no default constructor?
        Integer(int i);
        Integer(std::string str);
    
        int getValue();
        void setValue(int i);
    
        Integer& operator+( Integer const& arg); // why use 'Integer' AND 'int' ?!
    
      private:
        int myValue;
    };
    
    class Double
    {
      public:
        Double(); // don't you want a default constructor?
        Double(double i); // not explicit to cast implicitely Double to Integer
        Double(std::string str);
    
        double getValue();
        void setValue(double i);
    
      private:
        double myValue;
    };
    
    // ***
    
    Integer& operator+( Integer const& arg1, Integer const& arg2)
    {
        Integer result = arg1.myValue + arg2.myValue;
        return result;
    }
    
    Double& operator+( Integer const& arg1, Double const& arg2)
    {
        Double result = arg1.myValue + arg2.myValue;
        return result;
    }
    
    Double& operator+( Double const& arg1, Integer const& arg2)
    {
        Double result = arg1.myValue+ arg2.myValue;
        return result;
    }
    
    Double& operator+( Double const& arg1, Double const& arg2)
    {
        Double result = arg1.myValue + arg2.myValue;
        return result;
    }
    Ok, only the operator with the two 'Double' arguments would have sufficed, since implicit casts are allowed (no 'explicit' keyword in the one-argument constructor). But to make the point...

    And any 'operator@' should be defined, if possible, in the canonical way by calling the member 'operator@=' (this _must_ be a member, like '=').


    EDIT: try to be as general as possible. Your class is called 'Integer', so why call the private attribute 'myInteger' ? Let's call it 'myValue'.

    EDIT2: is #include <iostream> really always necessary?
    Last edited by MarkZWEERS; 05-14-2008 at 06:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 03-30-2009, 06:37 PM
  2. no match for 'operator>>'
    By Taka in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2009, 12:17 AM
  3. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM
  4. 2 array match
    By ajastru2000 in forum C++ Programming
    Replies: 5
    Last Post: 07-18-2003, 07:58 AM
  5. How do I match 2 files to print data.
    By sketchit in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-12-2001, 05:45 PM