C++操作员问题
我不知道这个问题在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
与其他答案的建议相反,您不需要提供一个专门的
operator-
来接受int
和Timestamp
,而是您可以(可能应该)使operator-
成为非成员函数:这样编译器就可以自由地将转换应用于左侧和右侧操作数,并使用来自 <代码> int 到
时间戳
。您可以在此处阅读有关运算符重载的设计和实现的简短说明,或者您可以在 SO 中的 [C++-faq] 标签中搜索“运算符重载”。
Contrary to what the other answers propose, you do not need to provide an specialized
operator-
that takes anint
and aTimestamp
, rather you can (and probably should) makeoperator-
a non-member function: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
toTimestamp
.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".
您需要定义以下非成员函数:
顺便说一句,将成员
operator-()
设置为 const 函数:如果将此设置为 const,则只有
right-left
> 可以工作,因为right
是 const 对象,因此只能在其上调用 const 成员函数。我注意到您有一个以
int
作为参数的构造函数。此构造函数的行为类似于从int
到TimeStamp
的隐式转换函数,这意味着,您不需要定义两个operator-()
函数(一个作为您已经定义的成员函数,另一个作为我上面建议的非成员函数)。相反,您只能定义一个这种类型的 friend 函数:您必须将该类设为
friend
,因为它需要访问private
> 班级成员。然而,@David 提供了更好的解决方案。但我保留这个解决方案仅用于学术目的。You need to define the following non-member function:
By the way, make the member
operator-()
const function as:If you make this const, only the
right-left
would work, becauseright
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 fromint
toTimeStamp
which means, you don't need to define twooperator-()
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:You've to make
friend
of the class, as it needs to access theprivate
members of the class. However, @David provided a better solution. But I'm keeping this solution only for academic purpose.我看到两件事:
首先,你的构造函数采用 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!
您的
Timestamp::operator-(int)
允许您从 Timestamp 对象中减去整数。它不允许您从整数中减去时间戳。您需要添加operator-作为免费函数,
这需要成为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
This needs to be a friend of Timestamp as it needs to access the private members.