Thread: Operator Overloading...

  1. #1
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86

    Operator Overloading...

    Hey guys I have this problem where I'm trying to use the "++" operator with objects. I'm new to classes and I don't know exactly where I can find the problem so I'm posting my whole code...
    Code:
    #include<iomanip.h>
    
    
     class Date
     {
     private:
       int month;
       int day;
       int year;
     public:
       Date(int=1,int=1,int=2001);
       Date(long);
       Date(const Date&);
       Date operator++(int);
       void setdate(int, int, int);
       void showdate();
     };
    
    
    Date::Date(int mm, int dd, int yyyy)
    {
      month=mm;
      day=dd;
      year=yyyy;
    }
    
    
    Date::Date(long yyyymmdd)
    { 
      year=int(yyyymmdd/10000);
      month=int((yyyymmdd-year*10000)/100);
      day=int(yyyymmdd-year*10000-month*100);
    }
    
    
    
    
    
    Date::Date(const Date& olddate)
    { 
      month=olddate.month;
      day=olddate.day;
      year=olddate.year;
    }
    
    Date Date::operator++(int day)
    {
     day=day+1;
     
    } 
    
    
    void Date::setdate(int mm, int dd, int yyyy)
    { 
      month =mm;
      day=dd;
      year=yyyy;
     
      return;
    }
    
    
    
    
    void Date::showdate()
    { 
      cout<<setfill('0');
      cout<<setw(2)<<month<<'/'
          <<setw(2)<<day<<'/'
          <<setw(2)<<year<<" ";
    }
    
    
     main()
    { int i;
     Date d1,d2(03,21,2002);
     Date d3(d1);
     Date d4=d2;
     Date d5(20020507L);
     
     cout<<"Date 1="; d1.showdate();  cout<<"Date 2=";d2.showdate();
     cout<<"Date 3="; d3.showdate();  cout<<"Date 4=";d4.showdate();
     cout<<"Date 5="; d5.showdate();  cout<<endl;
     
     ++d1;/* d2=d2+30; d3++; i=d2-d4; d5=d1;
     
      cout<<"Date 1="<<d1.showdate(); cout<<"Date 2="<<d2.showdate();
      cout<<"Date 3="<<d3.showdate(); cout<<"Date 4="<<d4.showdate();
      cout<<"Date 5="<<d5.showdate(); cout<<endl;
      cout<<"i="<<i<<endl;
    
      d1--; --d3; d5=20020507L;
     
      if(d1==d3)
        cout<<"Date 1 and Date 3 are equal\n";
      else 
        cout<<"Date 1 and Date 3 are not equal\n";
      
      if(d2>d4)
        cout<<"Date 2 is greater than Date 4\n";
      else
        cout<<"Date 2 is not greater than Date 4\n";
      
      cout<<"Date 1="<<d1.showdate(); cout<<"Date 2="<<d2.showdate();
      cout<<"Date 3="<<d3.showdate(); cout<<"Date 4="<<d4.showdate();
      cout<<"Date 5="<<d5.showdate(); cout<<endl;
     */
     return 0;
    }
    The compiler gives me these errors:

    lb05fa02.C: In function `int main()':
    lb05fa02.C:91: no match for `++Date &'
    lb05fa02.C:53: candidates are: class Date Date::operator ++(int) line 53 only has an opening brace. The function header is on 52.

    Any help is greatly appreciated
    "For in fact what is a man in Nature? A Nothing in comparison with the Infinite, an All in comparison with the Nothing, a mean between nothing and everything"- Blaise Pascal

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I didn't look through the entire code but I do see one problem with your overloaded operator.

    Code:
    Date Date::operator++(int day)
    {
     day=day+1;
    
    }
    This is what you have. You clearly say you will return a Date object but yet you have no return statement. Try writing the code like this.

    Code:
    Date Date::operator++(int day)
    {
      day = day + 1; // or ++day;
      return *this;
    }
    That should work.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    You have an ambiguity there. The int passed to the function is titled day, but so is the int that represents the day number internally. You don't need the day parameter to the overloaded function.

    Code:
    Date Date::operator++(void)
    {
      ++day;// or day=day+1;
      return *this;
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your postfix increment is faulty in several ways.

    >Date Date::operator++(int day)
    The return value should be const so that you can avoid confusing syntax like

    d1++++;

    Also, don't name the parameter, that is only there to tell the compiler that you are overloading the postfix operator and not the prefix operator. You also need to return a value. The correct working of the postfix increment operator is to save the old value, increment the current value and then return the old value as a constant:
    Code:
    const Date Date::operator++ ( int )
    {
      const Date old = *this; // Save the old Date
    
      ++day; // Increment the day
    
      return old; // return the old Date
    }
    >++d1;
    Here you try to use the prefix increment operator yet you've only implemented the postfix, so this won't work. Change it to

    d1++;

    And that problem will be fixed.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed