operator overloading - operator+

I've created a time class, which I would like to add the operator+ to.
I'm getting an error though:
1
2
3
4
5
$ make
g++ -c -Wall -std=gnu++11 time.cpp
In file included from time.cpp:2:0:
time.h:15:45: error: ‘Time& Time::operator+(Time&, const Time&)’ must take either zero or one argument
     Time& operator+(Time & s, const Time & t)

The files are as follows:
time.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef TIME0_H_
#define TIME0_H_
class Time {
  private:
    int hours;
    int minutes;
  public:
    Time();
    Time(int h, int m = 0);
    void AddMin(int m);
    void AddHr(int h);
    void Reset(int h = 0, int m = 0);
    Time& operator+(Time & s, const Time & t)
    void Show() const;
};
#endif 

time.cpp
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
#include <iostream>
#include "time.h"

Time::Time() {
  hours = minutes = 0;
}

Time::Time(int h, int m) {
  this->hours = h;
  this->minutes = m;
}

void Time::AddMin(int m) {
  this->minutes += m;
  this->hours += this->minutes / 60;
}

void Time::AddHr(int h) {
  this->hours += h;
}

void Time::Reset(int h, int m) {
  this->hours = h;
  this->minutes = m;
}

Time& Time::operator+(Time & s, const Time & t) { // line is highlighted as the compile error
  s.minutes = s.minutes + t.minutes;
  s.hours = s.hours + t.hours + (s.minutes / 60);
  s.minutes %= 60;
  return s;
}

void Time::Show() const {
  std::cout << this->hours << " hrs " << minutes << " min's" << std::endl;
}

time_usage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "time.h"
#include <iostream>

int main() {
  Time t1;
  Time t2;
  t1.AddHr(1);
  t1.AddMin(30);
  t2.AddMin(30);
  t1 = t2 + 30; // use of operator+
  t1.Show();

  return 0;
}

Any suggestions would be appreciated.
Search for C++ Primer (5th ed.) by Stephen Prata - Chapter 11 - Time on Our Hands: Developing an Operator Overloading Example - is just the thing you might be looking for
Topic archived. No new replies allowed.