在C ++类中使用带有三个参数的构造函数

时间:2014-10-25 22:37:49

标签: c++ class

我在C ++中创建一个名为Time的类,该类有三个整数作为私有成员变量。我很擅长在C ++中使用类,并试图找出如何解决这个特定问题。问题在于,当我尝试这样做时:

cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;

我遇到编译器错误,我认为这是因为带有三个参数的构造函数需要以不同的方式编码,因为类中的私有变量从第一个构造函数中获取值,然后尝试从中减去第二个构造函数,所以第一个值丢失(我相信)。那么构造函数如何保持旧值,所以我可以进行减法而不将其存储在命名变量中。

这是我的代码:

#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;

class Time
{
    private:
        int hours;
        int minutes;
        int seconds;
        void normalize();

    public:
        Time() {hours = minutes = seconds = 0; normalize();};
        Time(int x, int y, int z);
        friend Time operator + (const Time& t1, const Time& t2);
        friend Time operator - (const Time& t1, const Time& t2);
        friend bool operator < (const Time& t1, const Time& t2);
        friend istream& operator >>(istream& ins, Time& t1);
        friend ostream& operator <<(ostream& out, Time& t1);
};

Time::Time(int x, int y, int z)
{
    hours = x;
    minutes = y;
    seconds = z;

    normalize();
}

void Time::normalize()
{
    int s = seconds;
    int m = minutes;
    int h = hours;

    while (s < 0)
    {
        s += 60;
        m--;
    }

    while (m < 0)
    {
        m += 60;
        h--;
    }

    while (h < 0)
    {
        h = h + 24;
    }

    seconds = s % 60;
    minutes = (m + s/60) % 60;
    hours = (h + m/60 + s/3600) % 24;
}

istream& operator >>(istream& ins, Time& t1)
{
    ins >> t1.hours;
    ins >> t1.minutes;
    ins >> t1.seconds;

    t1.normalize();

    return ins;
}
ostream& operator <<(ostream& out, Time& t1)
{
    if (t1.hours < 10)
        out << '0' << t1.hours << ":";
    else
        out << t1.hours << ":";
    if (t1.minutes < 10)
        out << '0' << t1.minutes << ":";
    else
        out << t1.minutes << ":";
    if (t1.seconds < 10)
        out << '0' << t1.seconds;
    else
        out << t1.seconds;

    return out;
}

int main()
{
    Time t1, t2, t3, t4;
    cin >> t1;
    cin >> t2;
    cin >> t3;

    cout << "Time1: " << t1 << endl;
    cout << "Time2: " << t2 << endl;
    cout << "Time3: " << t3 << endl;

    t4 = t1 + t2;
    cout << "Time4: " << t4 << endl;

    t1 = t3 - t4;
    cout << "Time1: " << t1 << endl;

    if (t1 < t3)
        cout << "Time1 < Time3" << endl;
    else
        cout << "Time3 >= Time1" << endl;

    Time t5 = t2 + Time(0,0,1);
    if (t5 < t2)
        cout << "Time5 < Time2" << endl;
    else
        cout << "Time5 >= Time2" << endl;

    cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;

    return 0;
}

Time operator + (const Time& t1, const Time& t2)
{
    Time temp;
    temp.hours = t1.hours + t2.hours;
    temp.minutes = t1.minutes + t2.minutes;
    temp.seconds = t1.seconds + t2.seconds;

    return temp;
}

Time operator - (const Time& t1, const Time& t2)
{
    Time temp;

    temp.hours = t1.hours - t2.hours;
    temp.minutes = t1.minutes - t2.minutes;
    temp.seconds = t1.seconds - t2.seconds;

    temp.normalize();

    return temp;
}

bool operator < (const Time& t1, const Time& t2)
{
    if (t1.hours < t2.hours && t1.minutes < t2.minutes && t1.seconds < t2.seconds)
        return true;
    else
        return false;
}

5 个答案:

答案 0 :(得分:1)

构造函数很好。您遗失的是operator-operator<<的合适重载。定义它们。

答案 1 :(得分:0)

ostream& operator <<(ostream& out, Time& t1)
{
    if(t1.hours < 10)
        out << "0" << t1.hours << ":";
    else
        out << t1.hours << ":";
    if(t1.minutes < 10)
        out << "0" << t1.minutes << ":";
    else
        out << t1.minutes << ":";
    if(t1.seconds < 10)
        out << "0" << t1.seconds;
    else
        out << t1.seconds;
}

Time operator - (const Time& t1, const Time& t2)
{
    Time temp;

    temp.hours = t1.hours - t2.hours;
    temp.minutes = t1.minutes - t2.minutes;
    temp.seconds = t1.seconds - t2.seconds;

    temp.normalize();

    return temp;
}

这些是 - 运算符函数和ostream&amp;操作员功能

答案 2 :(得分:0)

您必须重载运算符减去operator-和运算符&lt;&lt; operator<<

对于运营商这样做:

Time& Time::operator-(Time const& time){
     hours = time.getHours();
     minutes = time.getMinutes();
     seconds = time.getSeconds();
    return *this;
}

答案 3 :(得分:0)

在这种情况下,编译器不知道如何在cout中显示你的类,并且它不知道你想如何减去2个类,你需要告诉编译器它应该如何工作,重载运算符。

您需要定义运算符&lt;&lt;让你的班级在ostream中得到恰当的展示。 并且你需要重载 - 运算符来进行减法。

如何重载&lt;&lt;为你自己上课:How to properly overload the << operator for an ostream?

有关运算符重载的详细文档:http://en.cppreference.com/w/cpp/language/operators

答案 4 :(得分:0)

您没有超载您的运营商&lt;&lt;正确的,因为你忘了返回一个值。

以下程序编译并运行且没有错误。我删除了normalize功能,但仍然没有问题:

#include <ostream>
#include <iostream>

struct Time
{
    int hours;
    int minutes;
    int seconds;
    Time(int h=0, int m=0, int s=0) : hours(h), minutes(m), seconds(s) {}
};

std::ostream& operator <<(std::ostream& out, const Time& t1)
{
    if (t1.hours < 10)
        out << "0" << t1.hours << ":";
    else
        out << t1.hours << ":";
    if (t1.minutes < 10)
        out << "0" << t1.minutes << ":";
    else
        out << t1.minutes << ":";
    if (t1.seconds < 10)
        out << "0" << t1.seconds;
    else
        out << t1.seconds;
    return out;
}


Time operator - (const Time& t1, const Time& t2)
{
    Time temp;
    temp.hours = t1.hours - t2.hours;
    temp.minutes = t1.minutes - t2.minutes;
    temp.seconds = t1.seconds - t2.seconds;
    return temp;
}

using namespace std;

int main()
{
    cout << "Almost midnight: " << Time(0, 0, 0) - Time(0, 0, 1) << endl;
}

输出:

Almost midnight: 00:00:0-1

现在,如果输出不正确,那么问题与您发布的问题不同。您的原始问题涉及编译器错误,上面的代码没有这样的错误。但是,它确实包含不返回值的修复程序。

除此之外,请注意operator <<的参数是const引用,而不是引用。这将允许返回临时值的表达式在operator <<中使用。

显示const如何产生影响:如果我们采用您的原始代码(在Time&的实施中使用const Time&代替operator <<),而是将Time对象的减法值存储到非临时对象中,您将看到代码将成功编译:

   Time tSub = Time(0,0,0) - Time(0,0,1); 
   cout << "Almost midnight: " << tSub << endl;

虽然这不会成功编译:

   cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;

所以问题是原始代码中的operator <<仅适用于非const对象。通过创建参数const Time&,可以缓解这些错误。