C++操作员问题

发布于 2024-12-06 17:32:39 字数 1484 浏览 1 评论 0原文

我不知道这个问题在 stackoverflow 上是否已经得到解答。但我根本找不到合适的关键词来搜索。

我在下面插入了一些代码的精简版本。

所以基本上我在 main() 中尝试做的就是从 t2 中减去 122。我想我的编译器会自动将整数转换为 Timestamp 对象,然后减去它,如“Timestamp.cpp”中所示。

但是当它到达 t4 时它不会转换它并给我以下错误:

“722 - t1”中的“operator-”不匹配

我 100% 确定这是可能的。但如何呢?

也许我在转换方面完全错误......所以请犹豫纠正我,我正在努力学习一些东西。

精简代码:

main.cpp:

#include <iostream>
#include <iomanip>

#include "Timestamp.h"

using namespace std;

int main() {
    Timestamp t3(t2 - 122);
    cout << "T3 = " << t3 << endl;
    Timestamp t4(722 - t1);
    cout << "T4 = " << t4 << endl;

    return 0;
}

Timestamp.h

#ifndef TIJDSDUUR_H
#define TIJDSDUUR_H

using namespace std;

class Timestamp {
    public:
        Timestamp(int);
        Timestamp operator- (const Timestamp &t);
    private:
        int hour;
        int min;
};

Timestamp.cpp

Timestamp::Timestamp(int m) : hour(0), min(m) {

}

Timestamp Timestamp::operator- (const Timestamp &t) {
    Timestamp temp;

    temp.hour = hour;
    temp.min = min;

    temp.hour -= t.hour;
    temp.min -= t.min;

    while(temp.min < 0.00) {
        temp.hour--;
        temp.min += 60;
    }

    return temp;
}

I don't know if this question is already answered on stackoverflow. But I simply can not find the right keyword to search for.

I inserted some stripped down version of my code below.

So basically what I'm trying to do in my main(), is to subtract 122 from t2. I suppose my compiler converts the integer to a Timestamp object automatically and then subtracts it as showed in 'Timestamp.cpp'.

But when it arrives at t4 it doesn't convert it and give me the following error:

no match for 'operator-' in '722 - t1'

I'm 100% sure that it is possible. But how?

Maybe I'm totally wrong about converting... So please do hesitate to correct me, I'm trying to learn something.

STRIPPED DOWN CODE:

main.cpp:

#include <iostream>
#include <iomanip>

#include "Timestamp.h"

using namespace std;

int main() {
    Timestamp t3(t2 - 122);
    cout << "T3 = " << t3 << endl;
    Timestamp t4(722 - t1);
    cout << "T4 = " << t4 << endl;

    return 0;
}

Timestamp.h

#ifndef TIJDSDUUR_H
#define TIJDSDUUR_H

using namespace std;

class Timestamp {
    public:
        Timestamp(int);
        Timestamp operator- (const Timestamp &t);
    private:
        int hour;
        int min;
};

Timestamp.cpp

Timestamp::Timestamp(int m) : hour(0), min(m) {

}

Timestamp Timestamp::operator- (const Timestamp &t) {
    Timestamp temp;

    temp.hour = hour;
    temp.min = min;

    temp.hour -= t.hour;
    temp.min -= t.min;

    while(temp.min < 0.00) {
        temp.hour--;
        temp.min += 60;
    }

    return temp;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

み青杉依旧 2024-12-13 17:32:39

与其他答案的建议相反,您不需要提供一个专门的 operator- 来接受 intTimestamp,而是您可以(可能应该)使 operator- 成为非成员函数:

Timestamp operator-( Timestamp lhs, Timestamp const & rhs ); // Returns a timestamp?? really??

这样编译器就可以自由地将转换应用于左侧和右侧操作数,并使用来自 <代码> int 到时间戳

您可以在此处阅读有关运算符重载的设计和实现的简短说明,或者您可以在 SO 中的 [C++-faq] 标签中搜索“运算符重载”。

Contrary to what the other answers propose, you do not need to provide an specialized operator- that takes an int and a Timestamp, rather you can (and probably should) make operator- a non-member function:

Timestamp operator-( Timestamp lhs, Timestamp const & rhs ); // Returns a timestamp?? really??

That way the compiler is free to apply conversions to both the left hand side and the right hand side operands, and use the implicit conversion from int to Timestamp.

You can read a short description on design and implementation of operator overloads here, or you can search in the [C++-faq] tag in SO for "operator overload".

捂风挽笑 2024-12-13 17:32:39

您需要定义以下非成员函数:

Timestamp operator-(int left, const Timestamp &right)
{
    //define
}

顺便说一句,将成员 operator-() 设置为 const 函数:

Timestamp operator-(const Timestamp &t) const;
                                       //^^^^^ this makes the function const

如果将此设置为 const,则只有 right-left > 可以工作,因为 right 是 const 对象,因此只能在其上调用 const 成员函数。


我注意到您有一个以 int 作为参数的构造函数。此构造函数的行为类似于从 intTimeStamp 的隐式转换函数,这意味着,您不需要定义两个 operator-() 函数(一个作为您已经定义的成员函数,另一个作为我上面建议的非成员函数)。相反,您只能定义一个这种类型的 friend 函数:

Timestamp operator-(const Timestamp &left, const Timestamp &right)
{
  //define
}

您必须将该类设为 friend,因为它需要访问 private > 班级成员。然而,@David 提供了更好的解决方案。但我保留这个解决方案仅用于学术目的。

You need to define the following non-member function:

Timestamp operator-(int left, const Timestamp &right)
{
    //define
}

By the way, make the member operator-() const function as:

Timestamp operator-(const Timestamp &t) const;
                                       //^^^^^ this makes the function const

If you make this const, only the right-left would work, because right is const object, and so only const member function can be invoked on it.


I noticed that you've a constructor that takes int as parameter. This constructor can behave like implicit conversion function from int to TimeStamp which means, you don't need to define two operator-() functions (one as member function which you already have defined, and other as non-member function as I suggested above). Instead, you can define only one friend function of this type:

Timestamp operator-(const Timestamp &left, const Timestamp &right)
{
  //define
}

You've to make friend of the class, as it needs to access the private members of the class. However, @David provided a better solution. But I'm keeping this solution only for academic purpose.

云归处 2024-12-13 17:32:39

我看到两件事:

首先,你的构造函数采用 int 类型的参数。您的运算符返回类型 TimeStamp,然后将其传递给构造函数。您尚未指定从 TimeStamp 到 int 的转换,因此这不起作用!

其次,您的运算符采用时间戳类型的参数,但当您实际使用它时,您似乎从中减去了一个 int 。但它不知道 int 是什么,因为你没有告诉它!

Two things I see:

First, your constructor is taking a parameter of type int. Your operator is returning type TimeStamp, which you're then passing to the constructor. You haven't specified a conversion from TimeStamp to int, so this won't work!

Second, your operator is taking a parameter of type timestamp, but you appear to subtracting an int from it when you're actually using it. It has no idea what an int is though, because you didn't tell it!

删除会话 2024-12-13 17:32:39

您的 Timestamp::operator-(int) 允许您从 Timestamp 对象中减去整数。它不允许您从整数中减去时间戳。

您需要添加operator-作为免费函数,

Timestamp operator-(int, const Timestamp&);

这需要成为Timestamp的朋友,因为它需要访问私有成员。

Your Timestamp::operator-(int) allows you to subtract an integer from a Timestamp object. It does not allow you to subtract Timestamps from integers.

You need to add operator- as a free function

Timestamp operator-(int, const Timestamp&);

This needs to be a friend of Timestamp as it needs to access the private members.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文