Thread: Overloading operators some problems with (*,/,=) operators.

  1. #1
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34

    Overloading operators some problems with (*,/,=) operators.

    Hello i am doing some exercises with operators to train them, and I have got problem with those three cause they work bad for me
    (machine doesnt accept actions with this operators)
    I can use only 2 times double.
    Here is my code:
    Code:
    class MayCal
    {
     protected:
          double liczba;
          
     public:
     MayCal()
     {
     liczba = 0;
    };
     MayCal(double a)
     {
     liczba = a;
    }
    MayCal operator/ (MayCal const&);
    MayCal operator* (MayCal const&);
    MayCal & operator =(MayCal const& );
    MayCal MayCal::operator/ (MayCal const& ex)
    {    
    if(ex.liczba == 0)
    {
         
       MayCal tmp(liczba = 2012);
       
      return tmp;
         
       
    }
    else
    {
           MayCal tmp(liczba/ex.liczba);
       return tmp;  
    }    
    
    MayCal MayCal::operator* (MayCal const& ex)
    {    
       MayCal tmp(liczba * ex.liczba);
       return tmp;    
    }
    MayCal& MayCal::operator= (MayCal const& ex)
    {
         liczba = ex.liczba;
    }
    In operator/ I have got If cause in exercise always when i do divison
    MyCal a = 500 , c = 0;
    a/c it must give 2012
    500/0 = must give 2012.

    I think i got something bad with this division operator on my examples it work and operator= completly doesnt work and operator* works too on examples but machine tests both operator* and operator/ so i dont know which is bad or maybe both of them.
    On my examples operator* and operator/ works, but machine says it gives bad answer, and operator= totally doesnt work.

    When i do in main()
    Code:
    MayCal a = 50 , b = 30;
    cout << "a++ = " << a=b << endl;
    I get error [Error] no match for 'operator<<' in 'b << std::endl'

    I dont know that operator= is wrong made or my operator << doesnt work for this example.

    Could somebody look at those code? I will be very thankful.

    I add machine accepts for me operator+, operator- which have got same build as operator* and operator/ i think something is wrong with my division.
    Last edited by aatrox; 03-30-2014 at 08:06 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    operator << needs to be overloaded if you intend to do

    cout << mycal << endl;

    or any statement close to that.

    The only other thing I will say is that you do not need to overload operator=. The one that the compiler generates for you will work just fine this time.

  3. #3
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Operator << i got made like this, it works for examples like a+b , a*b , a/b but for a=b it doesnt (here is a error which i pasted) work in exercise i had to make assignment operator too for some other tests. I send only MyCal.h and machine test all operators on other main files and tests for assignement operators give bad answer.
    Code:
     friend ostream& operator<< (ostream&,MayCal const&);
    ostream& operator<< (ostream &wyjscie, MayCal const& ex)
    {
    
    
       wyjscie << ex.liczba;
    
    
       return wyjscie;
    
    
    }
    and i dont know what is wrong my operator= or operator<<

    And also operator/ and operator* are not accepted i dont know why because for my examples they work.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    A definition of operator*() like this
    Code:
    MayCal MayCal::operator* (MayCal const& ex)
    {   
       MayCal tmp(liczba * ex.liczba);
       return tmp;   
    }
    has to appear outside the class definition.
    Code:
    class MayCal
    {
             //    the definition in the form above cannot be in here
    };
    
    // it can be here
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Ok i have repaired my operator/ and operator* it had to be friend and take 2 args, but still i dont know how to repair assignment operator= , i got all the time bad answers for this one.
    Now it looks like
    Code:
    friend MayCal operator*(const MayCal& a,const MayCal& b){

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    All assignment operators have lower precedence than operator <<. So this
    Code:
    MayCal a = 50 , b = 30;
    cout << "a++ = " << a=b << endl;
    is equivalent to
    Code:
    MayCal a = 50 , b = 30;
    cout << "a++ = " << a= (b << endl);
    which requires class MyCal to have an operator<<() that accepts a stream manipulator - which yours does not. You probably intended something like
    Code:
    MayCal a = 50 , b = 30;
    cout << "a++ = " << (a= b) << endl;    // note brackets
    For that to work, MyCal's assignment operator needs to return a reference to something (otherwise the result of the assignment cannot be streamed to cout). Currently, your operator=() does not have a return statement, so the function returns an invalid reference to the caller. That causes the "cout .... "statement to exhibit undefined behaviour (since, to output something, cout's streaming operator accesses a non-existent object via the reference).

    And your MyCal needs to have a friend operator<<() in order to be streamed.
    Last edited by grumpy; 03-31-2014 at 06:42 AM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Ok it helped me thanks, and i have got next question with operator[] which i need also for my class which i presented here, i cant use any int, double and i must do operator[] for MayCal liczba, which will return round number like
    Code:
    MayCal a(0.12345678);
     cout << a[0] << " " << a[2] << " " << a[4];It will Output 0    0.12      0.1235.
    I want try do it myself but i completly dont know how to make structure of this without using int, if somebody could do me only structure of this operator[] i will be very thankful (dont know what arguments i need to put in operator)
    Code:
    MayCal MayCal :: operator[]=(xxxxxx)
    {
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The suggested headers for all of the operators you can overload can be found here: https://en.wikipedia.org/wiki/Operat..._C%2B%2B#Table

    The subscript operator looks like this:

    Code:
     R &K::operator [](S b);
    R& is your return type; K is the class name for which the operator is being overloaded, and S is the type of value inside the bracket. This operator is a member of the class, always.

  9. #9
    Registered User
    Join Date
    Nov 2013
    Location
    Poland
    Posts
    34
    Hmm i got question this operator[] should be something like this?
    Code:
    MayCal &operator[](MayCal y){
    MayCal r ;
    r.liczba= floor(this->liczba * pow(10,y.liczba))/pow(10,y.liczba) ;
    return r;
    }
    

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    No, not without a fairly liberal interpretation of "something like this".

    The variable r in your function is auto (i.e. it ceases to exist when the function returns) and your operator[] returns a reference to it. A function should never return a pointer or reference to an auto variable which is local to that function, since it will cause the caller to exhibit undefined if it uses that pointer or reference.

    Generally speaking, operator[] is only needed by classes that contain a collection of objects (e.g. an array) and need to provide caller access to elements of those collections. For example, if MayCal contains a member named x which is an array of 10 float, and the caller needs to access elements of that array, there are various types of operator[] that can be defined.

    Although not unheard of, it is unusual for an object to contain a collection of objects of the same type (for example, an apple does not generally contain a collection of apples).


    whiteflag's description, incidentally, is only one form of operator[]. There are others. The return value is not required to be a reference, for example (albeit, doing it that way imposes other constraints on the caller).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Overloading operators
    By ugmusicbiz in forum C++ Programming
    Replies: 2
    Last Post: 02-13-2009, 01:41 PM
  2. Help with using overloading operators
    By clueless159753 in forum C++ Programming
    Replies: 1
    Last Post: 12-10-2003, 11:48 AM
  3. Overloading Operators
    By Zoalord in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2003, 09:08 AM
  4. overloading operators..please help
    By Tozilla in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2003, 11:32 PM
  5. operators overloading
    By waqasriazpk in forum C++ Programming
    Replies: 1
    Last Post: 07-26-2002, 01:05 AM