Time class

So... I have some problems with my class that is supposed to do something with time... I get an error saying:
22 C:\xxx.cpp `const Time Time::operator-(const Time&, const Time&)' must take either zero or one argument

And here is my class:

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
class Time
{
      private :
              int hours, minutes, seconds;
      public  :
              Time(int h, int m, int s) { Set_Time(h, m, s); }
              Time() { Set_Time(0, 0, 0); }
              void Set_Time(int h, int m, int s);
              void Print_Time() { cout << " { " << hours << ":" << minutes << ":" << seconds << " } "; }
              
              int GetHours() const { return hours; }
              int GetMinutes() const { return minutes; }
              int GetSeconds() const { return seconds; }
              
              
              const Time operator -(const Time &VR_1, const Time &VR_2)
              {
                    if(VR_2.GetHours() > VR_1.GetHours() || VR_2.GetMinutes() > VR_1.GetMinutes() || VR_2.GetSeconds() > VR_1.GetSeconds())
                    {
                          throw "Mistake!\n";
                    }
                    else return Time(VR_1.GetHours() - VR_2.GetHours(), VR_1.GetMinutes() - VR_2.GetMinutes(), VR_1.GetSeconds() - VR_2.GetSeconds());
              }
};

int main()
{
    try
    {
          //My code here...
    }
    catch(const char* str) { cout << str; }
    getch();
    return 0;
}

void Time::Set_Time(int h, int m, int s)
{
     if(h > 23 || h < 0) throw "Mistake!\n";
     if(m > 59 || m < 0) throw "Mistake!\n";
     if(s > 59 || s < 0) throw "Mistake!\n";
     hours = h; minutes = m; seconds = s;
}


Thank you :)
You wrote a non-member function. However, operator- is a member function. The lefthand side (VR_1) is going to actually be "*this". So your protyotype should be:

const Time operator -(const Time &VR_2)

By the way--I'm not sure why you want the return type to be const.
How is it not a member function, it is inside the class body...
Anyway, if I remove "const Time &VR_1", it will report an error saying something about undeclared stuff.
Btw, I didn't learn anything about " *this " except that it is a pointer (obviously).

And for that const return type, it is just something that I didn't notice while I was typing... Thanks fo that too :P
What I meant was, it was written as if it were not a member function. You were writing a function that takes 2 Time references and returns a third. That is not really how a member function works. A member function would take a Time reference, add it to itself and return the result.

Read this tutorial. It should help. Specifically, read the CVector::operator+ example.

http://cplusplus.com/doc/tutorial/classes2/
Last edited on
I'm sort of new to c++ too, but I think you may be better served by NOT making the overloaded operator- function a member function. Instead, the way you intend to use the operator- function, it may be best to make it a "friend" of the Time class instead of a member function, and return a Time object. To make this function a friend, you'll need to declare it as such in the public section (without the implementation)

 
friend Time operator-(const Time& VR_1, const Time& VR_2);


You will have define the friend function outside of the class or in an implementation file
Last edited on
Topic archived. No new replies allowed.