c++ Undeclared identifier inside operator function

1k Views Asked by At

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.

2

There are 2 best solutions below

3
On BEST ANSWER

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() );
0
On

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() );