#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;*/
}
Add メソッドに問題があります。Xcode で次のエラーが表示されます:「TimeUnit の初期化に一致するコンストラクターがありません」
誰かが私が間違っていることを教えてもらえますか? 方法を知っていることは文字通りすべて試しましたが、この方法でコンパイルすることさえできません。
教授からの指示は次のとおりです。
TimeUnit クラスは、分と秒で構成される時間を保持できる必要があります。次のメソッドが必要です。
分と秒をパラメーターとして受け取るコンストラクター ToString() - 時刻に相当する文字列を返す必要があります。「M分S秒」Test1 Simplify() - このメソッドは時間をかけて単純化する必要があります。秒が 60 秒以上の場合は、秒を 60 未満に減らし、分を増やす必要があります。たとえば、2 分 121 秒は 4 分 1 秒になります。Test2 Add(t2) - 2 つの時間の単純化された加算である新しい時間を返す必要があります。