1

I'm creating a "date" class in c++, which holds day, month and year variables and bunch of operator functions to use it with.

I have a date.h header and date.cpp for my class and one of the operator functions in date.cpp is giving me bunch of errors.

date.cpp (I want this operator-function to count days added and return a new date object and avoid changes to the original date object.)

date date::operator+(long days) const{

    date dTemp( date.getDay(), date.getMonth(), date.getYear() );

    for(int i=0;i<days;i++){

        //If days go over a months day count.
        if(dTemp.getDay() >= daysInMonth[dTemp.getMonth()]){
            dTemp.setDay(1);
            if(dTemp.getMonth() < 12){
                dTemp.setMonth(dTemp.getMonth() + 1);
            }
            else{
                //Changing a year.
                dTemp.setMonth(1);
                dTemp.setYear(dTemp.getYear() + 1);
            }

        }
        else{
            dTemp.setDay(dTemp.getDay() + 1);
        }
    }
    return dTemp;
}

Errors:

1>h:\c++\teht21\teht20\date.cpp(74): error C2143: syntax error : missing ')' before '.'
1>h:\c++\teht21\teht20\date.cpp(74): error C3484: syntax error: expected '->' before the return type
1>h:\c++\teht21\teht20\date.cpp(74): error C2061: syntax error : identifier 'getDay'
1>h:\c++\teht21\teht20\date.cpp(79): error C2065: 'dTemp' : undeclared identifier
1>h:\c++\teht21\teht20\date.cpp(79): error C2228: left of '.getDay' must have class/struct/union
1>          type is ''unknown-type''

Line 74 is:

date dTemp( date.getDay(), date.getMonth(), date.getYear() );

Any help is hugely appreciated. If you need to see more code, let me know.

Baburo
  • 55
  • 6

2 Answers2

3

If getDay(), getMonth(), getYear() are member functions and you want to call them on this then change:

date dTemp( date.getDay(), date.getMonth(), date.getYear() );

to:

date dTemp( getDay(), getMonth(), getYear() );
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • This seems to work, thank you very much. What is the difference between your answer and dbrank0's answer? Because both seems to work. Just curious because I've just started learning c++. – Baburo Jan 26 '12 at 11:25
  • 2
    @Baburo: the difference is that dbrank0's code fully-qualifies the function name. If `getDay` is a virtual function, and `operator+` is called on a derived class of `date`, then `date::getDay()` will call the version of the function in `date`, whereas `getDay()` will call any override in the derived class. Otherwise, it makes no difference, it's just a different way of specifying the same function. – Steve Jessop Jan 26 '12 at 11:41
  • 1
    Btw, you could also probably write `date dTemp(*this);`, which will use the copy constructor to create `dTemp` – Steve Jessop Jan 26 '12 at 11:44
2

Probably you want to call static methods here:

date dTemp( date.getDay(), date.getMonth(), date.getYear() );

So:

date dTemp( date::getDay(), date::getMonth(), date::getYear() );
dbrank0
  • 9,026
  • 2
  • 37
  • 55