user2122140
user2122140

Reputation:

Problems with Add method in TimeUnit class

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class TimeUnit
{
public:
    TimeUnit(int m, int s)
    {
        this -> minutes = m;
        this -> seconds = s;
    }

    string ToString()
    {
        ostringstream o;
        o << minutes << " minutes and " << seconds << " seconds." << endl;

        return o.str();
    }

    void Simplify()
    {
        if (seconds >= 60)
        {
            minutes += seconds / 60;
            seconds %= 60;
        }
    }

    TimeUnit Add(TimeUnit t2)
    {
        TimeUnit t3;

        t3.seconds = seconds + t2.seconds;

        if(t3.seconds >= 60)
        {
            t2.minutes += 1;
            t3.seconds -= 60;
        }

        t3.minutes = minutes + t2.minutes;

        return t3;
    }

private:
    int minutes;
    int seconds;

};

int main(){

    cout << "Hello World!" << endl;

    TimeUnit t1(2,30);
    cout << "Time1:" << t1.ToString() << endl;

    TimeUnit t2(3,119);
    cout << "Time2:" << t2.ToString();
    t2.Simplify();
    cout << " simplified: " << t2.ToString() << endl;

    cout << "Added: " << t1.Add(t2).ToString() << endl;
    //cout << " t1 + t2: " << (t1 + t2).ToString() << endl;

    /*cout << "Postfix increment: " << (t2++).ToString() << endl;
    cout << "After Postfix increment: " << t2.ToString() << endl;

     ++t2;
     cout << "Prefix increment: " << t2.ToString() << endl;*/

}

I'm having problems with my Add method. Xcode is giving me this error: "No matching constructor for initialization of TimeUnit"

Could someone please tell me what I am doing wrong? I've literally tried everything that I know how to do, but I can't even get it to compile with this method.

Here are the instructions from my professor:

The TimeUnit class should be able to hold a time consisting of Minutes and Seconds. It should have the following methods:

A constructor that takes a Minute and Second as parameters ToString() - Should return the string equivilant of the time. "M minutes S seconds." Test1 Simplify() - This method should take the time and simplify it. If the seconds is 60 seconds or over, it should reduce the seconds down to below 60 and increase the minutes. For example, 2 Min 121 seconds should become 4 minutes 1 second. Test2 Add(t2) - Should return a new time that is the simplified addition of the two times Test3 operator + should do the same thing as Add Test4 pre and postfix ++: should increase the time by 1 second and simplify Test5

Upvotes: 0

Views: 193

Answers (2)

billz
billz

Reputation: 45420

In your TimeUnit::Add function, you tried to initialize t3 with default constructor. However, your TimeUnit doesn't have one:

TimeUnit Add(TimeUnit t2)
{
   TimeUnit t3;   ///<<<---- here
   ///.....
}

Try update TimeUnit::Add to this way:

TimeUnit Add(const TimeUnit& t2)
{
   return TimeUnit(this->minutes+t2.minutes, this->seconds+t2.seconds);
}

Upvotes: 2

Brian Cain
Brian Cain

Reputation: 14619

The specific problem is because there is no TimeUnit::TimeUnit() defined, only TimeUnit(const int &m, const int &s).

Upvotes: 0

Related Questions