-1

I need to implement a static method to check if the date is correct. I created a static constant struct and initialized it, but the compiler throws: undefined reference to `Clock::max_vals'

It may be necessary to initialize this structure somehow differently, but I would not want the user to have access to it.

Here is the code

struct Date{
    unsigned long long years;
    unsigned short months;
    unsigned short days;
};

struct Time{
    unsigned short hours;
    unsigned short minutes;
    unsigned short seconds;
};

class Clock{
    private:
        static const struct max_values{
            unsigned long long max_years = 584942417355; //this num * 365 * 24 * 60 * 60 == max in this type 
            unsigned short max_months = 12;
            unsigned short max_days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
            unsigned short max_hours = 24;
            unsigned short max_minutes = 60;
            unsigned short max_seconds = 60;
        } max_vals;

        Date date;
        Time time;
    public:
        Clock();
        static bool correct_date(unsigned short days, unsigned short months, unsigned short years);
        void print();
};

Clock::Clock(){
    date = {0, 0, 0};
    time = {0, 0, 0};
}

bool Clock::correct_date(unsigned short days, unsigned short months, unsigned short years){
    if(max_vals.max_years < years || max_vals.max_months < months){
        return false;
    }
    return max_vals.max_days[months + 1] < days ? false : true;
}

int main(int argc, char const *argv[]){
    Clock timer;
    std::cout << timer.correct_date(23, 5, 2022) << std::endl; //worked if max_vals and correct_date not static
    return 0;
}

0 Answers0