probly a simple solution

This is a discussion on probly a simple solution within the C++ Programming forums, part of the General Programming Boards category; hi guys, can someone HELP I have a class that receives a start date and end date (ie. as 12 ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    probly a simple solution

    hi guys,
    can someone HELP

    I have a class that receives a start date and end date
    (ie. as 12 11 2005) and I have to print out the dates in between
    in form 12 November 2005.

    I have various functions that convert the date etc. but am stuck with defining

    operator++

    I really can't find any example that shows this.

    Here is what I have and the errors:

    MY CODE:

    Code:
     void date::operator++(date& oldDate)
    {
      unsigned maxDaysInMonth, maxDaysInFeb;
      unsigned dayOfMonth=oldDate.getDay();
      unsigned monthOfYear=oldDate.getMonth();
      unsigned myYear=oldDate.getYear();
    
      if (oldDate.isLeapYear(myYear))
        maxDaysInFeb=29;
      else
        maxDaysInFeb=28;
    
      switch(monthOfYear)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
          maxDaysInMonth=31;
        case 4: case 6: case 9: case 11:
          maxDaysInMonth=30;
        case 2:
          maxDaysInMonth=maxDaysInFeb;
        default:
         break;
        }
    
      if (maxDaysInMonth=dayOfMonth)
        {
          oldDate.setDay(1);
          if (monthOfYear=12)
            {
              oldDate.setMonth(1);
             oldDate.setYear(myYear+1);
            }
          else
            {
              oldDate.setMonth(monthOfYear+1);
             oldDate.setYear(myYear);
            }
        }
      else
        {
          oldDate.setDay(dayOfMonth+1);
          oldDate.setMonth(monthOfYear);
          oldDate.setYear(myYear);
        }
    
    }
    THE Errors:

    Code:
    date.h:35: error: postfix `void date::operator++(date&)' must take `int' as its
       argument
    date.cpp:135: error: postfix `void date::operator++(date&)' must take `int' as
       its argument

    I know this would be pretty straight forward but how can i make it work?

    thanks

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Are you sure in your member function declaration it does not pass a type int?

    Also, member functions can access private member variables, you do not have to use a function call to access it.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    not sure...

    I am not sure what you mean?
    my .h and .cpp have same names...

    my private members are
    unsigned day
    unsigned month
    unsigned year

    As for accessing private members, I tried to daclare day=oldDay.getDay() but this didnt work!!

  4. #4
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    In your class, what does the function declaration look like?

    As for accessing private members, I tried to daclare day=oldDay.getDay() but this didnt work!!
    A class function has access to private member variables.

    So instead of this:
    Code:
    unsigned myYear=oldDate.getYear();
    you could just use =oldDate.year;

    Unless your getYear/Day/Month function does something besides just return the private member variable.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    my member function (if this is what I think it is ) is
    date (unsigned newDay, unsigned newMonth, unsigned newYear)

    and my default sets day and month to 1 and year to 0000;

    Am I even answering the question?

    I just cant seem to understand what it means.

    I have tried to use
    int instead of void, a bool, ostream, but in all honesty, don't get it!!

    Thanks for the help, is this a really stupid and dumb question or do I just feel that way?

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    the function declaration:
    (in date.h)

    Code:
    void operator++(date& oldDate);

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    if (maxDaysInMonth=dayOfMonth)
    {
        oldDate.setDay(1);
        if (monthOfYear=12)
    I hope you mean == and not =.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I will fix this!!

    Thanks!!

    Will this help compiling?

  9. #9
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I have fixed this problem...

    Still, my error is:

    Code:
    date.h:36: error: postfix `bool date::operator++(date&)' must take `int' as its
       argument
    date.cpp:162: error: postfix `bool date::operator++(date&)' must take `int' as
       its argument
    even though I have changed to a bool fulnction that returns if the received date is valid.

    Can anyone see the problem?

    I am (obviously) pretty average at this, but I can't do anything else until I fix this code.

    Why must it take int as it's argument?

    I don't understand. Shouldn't it take true/ false as its argument?
    And a postfix: I have tried to declare it as unsigned function... this did not work...

  10. #10
    Absent Minded Programmer
    Join Date
    May 2005
    Posts
    964
    You're trying to pass a variable (bool) when you've voided it....

    try this
    Code:
    bool date::operator++(date& oldDate)
    since you're giving it something, when it is voided, its automatically declaring it as an integer...

    This may not be true, but tis my speculation


    Errrr... I just realized you've declared it as bool date already...


    Well, perhaps it wants a 0 or a 1, 1 meaning true, 0 meaning false????

    Sorry you lost me :\

  11. #11
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    I have totally lost me too!!

    You are totally right though, when I changed to a bool, the error changed to


    # must take either zero or one argument#

    Then, I changed it a bit more as

    date date:perator++(unsigned newDay, unsigned newMonth, unsigned newYear) and same error!!

    Is there anybody who has previously coded something like this?

    I am happy to recode the whole thing, but don't even know where to start!!

    This thing was due 4 days ago (there goes 40%) and it is just getting impossible to finish.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 'Solution' and 'Project' usage in VC++
    By C+/- in forum C++ Programming
    Replies: 2
    Last Post: 01-13-2007, 08:50 AM
  2. Solution to earlier quad-tree culling
    By VirtualAce in forum Game Programming
    Replies: 3
    Last Post: 08-10-2006, 08:04 PM
  3. ...deceptively simple...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 07-29-2002, 12:51 PM
  4. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 07:52 AM
  5. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21