#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類中添加方法的問題
有人能告訴我我做錯了什麼嗎?我已經嘗試了所有我知道如何去做的事情,但我甚至無法用它來編譯。
下面是我的教授的說明:
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
我不發佈指令較早道歉,但他們現在公佈。我不允許創建更多的構造函數。 – 2013-03-01 04:21:27