3

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

enter image description here

I don't understand what I did wrong. I think it's something stupid but I can't find it.

2
  • BTW you have a subtle bug: return _hours=tm.hours() && Commented Mar 31, 2013 at 9:54
  • Error clearly says time is not a type - thus causing trouble in time time::getTimeFromUser(). Add class time; before entire declaration of class time {}; Commented Mar 31, 2013 at 9:57

3 Answers 3

5

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 { ... } 
Sign up to request clarification or add additional context in comments.

Comments

1

First problem:

bool time::operator==(time tm) { return _hours=tm.hours() && _minutes==tm.minutes() && _seconds==tm.seconds(); // ^ } 

There should be a comparison operator there, not an assignment:

bool time::operator==(time tm) { return _hours==tm.hours() // ^^ && _minutes==tm.minutes() && _seconds==tm.seconds(); } 

Second problem:

You are using cout and endl, which belong to the std namespace, without a using declaration or a using directive.

You should add the following before using cout, cin, and endl as unqualified name:

using std::cout; using std::endl; using std::cin; 

Alternatively, you can choose to fully qualify their names when using them. For instance:

 std::cout<<"Error"<<endl; _hours=-1; _minutes=-1; _seconds=-1; // ^^^^^ 

3 Comments

While these are errors and it is good to point them out, none of them are the ones causing the compilation errors mentioned by the OP.
+1 For pointing out the errors which would have been ignored otherwise.
@AlokSave: You're right, I had to leave before I figured that out, so... my bad :)
1

the problemm is that your header is time.h (already take as a standard header). You think you get the one you declared but you get the standard one. thereofore, you don't have the declaration of your class but the standard header. Rename it Test.h and Tes.cpp and it will work And also add using namespace std on the top of your cpp file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.