2

So, I'm getting an undeclared identifier error in my member functions for my appointment class, occurs_on . I thought I initialized those variables in the constructor and the date object in the initializer list. But neither of the variables work. I'm just a bit confused why the compiler thinks it's an undeclared type and not a variable.

Thanks.

#include<iostream>
#include <string>


using namespace std;

class Date{

public:
    Date(int month, int day, int year);

    int getMonth() const;
    int getDay() const;
    int getYear() const;

private:
    int month;
    int day;
    int year;
};

Date::Date(int month, int day, int year) {
    this->month = month;
    this->day = day;
    this->year = year;
}

int Date::getMonth() const{
    return month;
}

int Date::getDay() const{
    return day;
}

int Date::getYear() const{
    return year;
}


class Appointment
{

    public:
    Appointment(string description, int month, int day, int year, int hour, int minute);
    virtual bool occurs_on(int month, int day, int year);

    private:
    int hour, minute;
    string convertInt(int number) const;
    virtual string print();

    protected:
    Date getDate();
    Date date;


};

Appointment::Appointment(string description, int month, int day, int year, int hour, int minute):date(month, day, year){
    // the above line, i'm trying to initalize the date object with the three parameters month day and year from the appointment constructor.
    this-> hour = hour;
    this-> minute =minute;

}

bool occurs_on(int month, int day, int year){
    if (date.getMonth()== month && date.getYear()= year && date.getDay()==day) //first error. variables like hour and minute from the constructor and date from the initalizer list are giving me unknown type name errors. I thought I initalized those variables in the constructor and in the initalizer list.

        day= minute; //

        return true;

}
Randomkid
  • 23
  • 3

3 Answers3

4

You have missed Appointment:: in front of occurs_on.

//---vvvvvvvvvvvvv
bool Appointment::occurs_on(int month, int day, int year){
    // ..
}
Kiril Kirov
  • 37,467
  • 22
  • 115
  • 187
4

You need the Appointment:: prefix:

bool Appointment::occurs_on(int month, int day, int year)

otherwise you are defining a free function.

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

As in the constructor definition, which is correct in your example, every (external) method definition needs the class name prefix before the method name it, which is in your case Appointment::.

Another option would be to define the method inline, which looks like this

class Appointment
{
    public:
// ...
    virtual bool occurs_on(int month, int day, int year){
        if (date.getMonth()== month && date.getYear()= year && date.getDay()==day)
            day= minute; // preparing a next question? ;)
            return true; // indentation error or even worse?
    }
    private:
    int hour, minute;
// ...
    Date date;
};

Defining methods inline may be helpful for local classes, but often it leads to bad style.

Wolf
  • 9,679
  • 7
  • 62
  • 108