C++ incomprehensible compiler errors

c++, compiler-errors

Solution

As it turns out, you are the victim of a subtle bug.

`time` is a function declared in the system header `time.h`, which is getting included in your program via `iostream`. When you declare `time time::getTimeFromUser()`, the compiler sees the return value and thinks you mean the function time!

`clang` makes this obvious with its error:

time.cpp:122:1: error: must use 'class' tag to refer to type 'time' in this scope
time time::getTimeFromUser()
^
class 
/usr/include/time.h:133:8: note: class 'time' is hidden by a non-type declaration of 'time' here
time_t time(time_t *);
       ^

The fix is to define that particular function like this:

class time time::getTimeFromUser() {
    ...
}

Or, if using C++11,

auto time::getTimeFromUser() -> time {
    ...
}

Problem

I got a pretty simple task to do. Write a class that defines time. For some reason, in one of the functions I get an error, that I do not understand. I searched for a solution with no success, so in the end I decided post it here. time.h ``` class time { private: int _hours; int _minutes; float _seconds; bool checkHours(int hours); bool checkMinutes(int minutes); bool checkSeconds(float seconds); public: time(int hours=0, int minutes=0, float seconds=0); time(const time & tm); ~time(); void hours(int hours); int hours() const; void minutes(int minutes); int minutes() const; void seconds(float seconds); float seconds() const; void operator=(time tm); bool operator==(time tm); void print(); time getTimeFromUser(); float getTimeAsFractionOfTheDay(time tm); ``` }; and time.cpp ``` #include <iostream> #include "time.h" bool time::checkHours(int hours) { return hours>=0 && hours<24; } bool time::checkMinutes(int MS) { return MS>=0 && MS<60; } bool time::checkSeconds(float MS) { return MS>=0 && MS<60; } //constractors time::time(int hours, int minutes, float seconds) { if(checkHours(hours) && checkMinutes(minutes) && checkSeconds(seconds)) { _hours=hours; _minutes=minutes; _seconds=seconds; } else { cout<<"Error"<<endl; _hours=-1; _minutes=-1; _seconds=-1; } } time::time(const time & tm) { _seconds = tm.seconds(); _hours = tm.hours(); _minutes=tm.minutes(); } time::~time() { } //get-set functions void time::hours(int hours) { _hours=hours; } int time::hours() const { return _hours; } void time::minutes(int minutes) { _minutes=minutes; } int time::minutes() const { return _minutes; } void time::seconds(float seconds) { _seconds = seconds; } float time::seconds() const { return _seconds; } //operators void time::operator=(time tm) { _hours=tm.hours(); _minutes=tm.minutes(); _seconds=tm.seconds(); } bool time::operator==(time tm) { return _hours=tm.hours() && _minutes==tm.minutes() && _seconds==tm.seconds(); } //some function void time::print() { cout<<" "<<_hours<<":"<<_minutes<<":"<<_seconds<<" "<<endl; } time time::getTimeFromUser() { time newTime; int userhours=-1; int userminutes=-1; float userseconds=-1; while (!checkHours(userhours)) { cout<<"enter hours"<<endl; cin>>userhours; if(!checkHours(userhours)) { cout<<"Error try again"<<endl; } } while (!checkMinutes(userminutes)) { cout<<"enter minutes"<<endl; cin>>userminutes; if(!checkMinutes(userminutes)) { cout<<"Error try again"<<endl; } } while (!checkSeconds(userseconds)) { cout<<"enter Seconds"<<endl; cin>>userseconds; if(!checkSeconds(userseconds)) { cout<<"Error try again"<<endl; } } newTime.seconds(userseconds); newTime.hours(userhours); newTime.minutes(userminutes); return newTime; } float time::getTimeAsFractionOfTheDay(time tm) { return 0.0; } ``` And i got those errors I don't understand what I did wrong. I think it's something stupid but I can't find it.

Original source