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:
    You will find them in Red

    lb05fa02.C: In function `int main()':
    lb05fa02.C:91: no match for `++Date &'
    lb05fa02.C:53: candidates are: class Date Date:perator ++(int)there's nothing except an opening brace on line 53. This function starts on line 52.

    Any help is 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
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    you're trying to pre-increment. I don't believe you can override that.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    way to post this thing twice
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Didn't mean to post it twice. i tried several times to submit post and I got this screen that said something about it not going through...and that an email has been sent to technical staff. sorry 'bout that.
    "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

  5. #5
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Try this :
    Code:
    Date& Date:operator++(Date& date)
    {
        day = day+1;
        return *this; 
    }

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    dog, that's preincrement? cool I should have known there was a way.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    No Luck.. the compiler says that the argument has to be of type int. I can't figure out why though
    "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

  8. #8
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> No Luck.. the compiler says that the argument has to be of type int. I can't figure out why thoughNo Luck.. the compiler says that the argument has to be of type int. I can't figure out why though

    Try changing the declaration of the function in the class as well, not just the definition.

    Code:
    class Date
     {
     private:
       int month;
       int day;
       int year;
     public:
       Date(int=1,int=1,int=2001);
       Date(long);
       Date(const Date&);
       Date& operator++(Date&);
       void setdate(int, int, int);
       void showdate();
     };

  9. #9
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    I tried that Dog.. still comes up with that error.
    "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

  10. #10
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Code:
    Date& Date::operator++(int date)
    {
            day = day + 1;
            return *this;
    }
    Change the declaration accordingly.

  11. #11
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    If I remember correctly, the int in this case doesn't technically mean integer, its just a way for the compiler to know you mean post-increment instead of pre-increment. This has something to do with the creators of all this loved to reuse commands instead of creating new ones. Try writing:
    Code:
    Date& operator++(int);  // defined within the class
      
    Date& Date::operator++(int)  // outside the class
    {
            day = day + 1;
            return *this;
    }
    If you wanted to to the pre-increment, then just do the same thing but without the int and leave the parenthesis blank. If this still gets an error, try making the function void instead of returning Date.

    I'm pretty sure this should work, although can't test it until I get to my computer at home!
    Last edited by PJYelton; 10-29-2002 at 05:25 PM.

  12. #12
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    PJYelton, you are correct.

    you're trying to pre-increment. I don't believe you can override that.
    Originally, no.
    But now it is possible, the two functions are.
    Code:
    Type& operator ++();       //prefix   ++x
    Type  operator ++(int);   //postfix x++
    Note that the int argument is just syntactic and is not to be used.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  13. #13
    Registered User ProgrammingDlux's Avatar
    Join Date
    Jan 2002
    Posts
    86
    Thanks a lot guys... I learned a lot from those comments. I appreciate it... Til next time

Popular pages Recent additions subscribe to a feed