operator overloading

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//.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)
Last edited on
Give more information about the compiler errors, your code looks fine.
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.
Last edited on
Very nice^^

Please mark your question as answered ;)
Topic archived. No new replies allowed.