Hi, having trouble with understanding operator overloading, everywhere there is theory explained but where it should be some more precise explanation always is (...).
So my task is, i have class time and need to be able to assign like this, time t,b;t=b; my code
//.h
class Time
{
public:
Time();
void setTime(int, int, int);
Time operator=(const Time&);
void printTime();
private:
int hour;
int minute;
int second;
};
//cpp
Time::Time()
{
hour=minute=second = 0;
}
void Time::setTime (int h, int m, int s)
{
hour = (h>=0 && h <24) ? h : 0;
minute = (m>=0 && m<60) ? m : 0;
second = (s>=0 && s<60) ? s : 0;
}
void Time::printTime()
{
cout << (hour < 10 ? "0" : "")<< hour <<":"
<< (minute < 10 ? "0" : "")<< minute <<":"
<< (second < 10 ? "0" : "")<< second;
}
Time Time::operator=(const Time &right)
{
this->hour=right.hour;
this->minute=right.minute;
this->second=right.second;
return *this;
}
//main
int main()
{
Time t, c;
t.setTime(1, 1, 1);
c.setTime(2, 2, 2);
t=c;
t.printTime();
cout<<endl;
}
there are more details in this, but everything else is working, except operator; when compiling i get pointed to this in makefile
$(BIN): $(OBJ)
$(CPP) $(LINKOBJ) -o $(BIN) $(LIBS)
weird, it suddenly worked; I am using dev c++ and it has some issues. Tried also codeblocks but somehow it doesn't want to work properly as well on win8 :(
I got error - id returned 1 exit status - all the time.
now i didn't change nothing, but opened another project parallel to this one. dev c++ just opens new window and now this one suddenly compiled properly.