丢弃限定符
我不太确定我的问题是什么。它说“放弃限定符”。我不知道为什么会发生这种情况。如果我删除重载运算符+=
的const
,一切都很好。有人可以帮我吗?
// practice on overloading operators on Time variables
#include <iostream>
using namespace std;
class Time {
public:
//constructor
Time(const unsigned int& day = 0, const unsigned int& hour = 0,
const unsigned int& minute = 0, const unsigned int& second = 0)
: day_(day),
hour_(hour),
minute_(minute),
second_(second) {
}
Time(const unsigned int& second = 0)
: day_(0),
hour_(0),
minute_(0),
second_(second) {
}
//copy constructor
Time(const Time& time)
: day_(time.day_),
hour_(time.hour_),
minute_(time.minute_),
second_(time.second_) {
}
//assignment operator
Time& operator=(const Time& time) {
day_ = time.day_;
hour_ = time.hour_;
minute_ = time.minute_;
second_ = time.second_;
return *this;
}
//destructor
~Time() {}
//overloaded operators
Time& operator+=(const Time& time);
Time operator+(const Time& time);
private:
unsigned int day_;
unsigned int hour_;
unsigned int minute_;
unsigned int second_;
void ConvertSecondsToTime();
unsigned int TotalTimeInSeconds();
};
//main function
int main() {
return 0;
}
//overloaded operators
unsigned int Time::TotalTimeInSeconds() {
return (day_ * 24 * 60 * 60 +
hour_ * 60 * 60 +
minute_ * 60 +
second_);
}
void Time::ConvertSecondsToTime() {
while (second_ >= 60) {
second_ -= 60;
minute_ += 1;
}
while (minute_ >= 60) {
minute_ -= 60;
hour_ += 1;
}
while (hour_ >= 24) {
hour_ -= 24;
day_ += 1;
}
}
Time& Time::operator+=(const Time& time) {
second_ = this->TotalTimeInSeconds() + time.TotalTimeInSeconds();
ConvertSecondsToTime();
return *this;
}
Time Time::operator+(const Time& time) {
Time temp(*this);
temp += time;
return temp;
}
1,3 Top
我的输出是:
time_overloaded_operators.cpp: In member function ‘Time& Time::operator+=(const Time&)’:
time_overloaded_operators.cpp:75: error: passing ‘const Time’ as ‘this’ argument of ‘unsigned int Time::TotalTimeInSeconds()’ discards qualifiers
I am not really sure what my problem is. it says "discards qualifier". I don't know why this happens. if I erase const
for overloaded operator+=
everything is fine. can someone help me out?
// practice on overloading operators on Time variables
#include <iostream>
using namespace std;
class Time {
public:
//constructor
Time(const unsigned int& day = 0, const unsigned int& hour = 0,
const unsigned int& minute = 0, const unsigned int& second = 0)
: day_(day),
hour_(hour),
minute_(minute),
second_(second) {
}
Time(const unsigned int& second = 0)
: day_(0),
hour_(0),
minute_(0),
second_(second) {
}
//copy constructor
Time(const Time& time)
: day_(time.day_),
hour_(time.hour_),
minute_(time.minute_),
second_(time.second_) {
}
//assignment operator
Time& operator=(const Time& time) {
day_ = time.day_;
hour_ = time.hour_;
minute_ = time.minute_;
second_ = time.second_;
return *this;
}
//destructor
~Time() {}
//overloaded operators
Time& operator+=(const Time& time);
Time operator+(const Time& time);
private:
unsigned int day_;
unsigned int hour_;
unsigned int minute_;
unsigned int second_;
void ConvertSecondsToTime();
unsigned int TotalTimeInSeconds();
};
//main function
int main() {
return 0;
}
//overloaded operators
unsigned int Time::TotalTimeInSeconds() {
return (day_ * 24 * 60 * 60 +
hour_ * 60 * 60 +
minute_ * 60 +
second_);
}
void Time::ConvertSecondsToTime() {
while (second_ >= 60) {
second_ -= 60;
minute_ += 1;
}
while (minute_ >= 60) {
minute_ -= 60;
hour_ += 1;
}
while (hour_ >= 24) {
hour_ -= 24;
day_ += 1;
}
}
Time& Time::operator+=(const Time& time) {
second_ = this->TotalTimeInSeconds() + time.TotalTimeInSeconds();
ConvertSecondsToTime();
return *this;
}
Time Time::operator+(const Time& time) {
Time temp(*this);
temp += time;
return temp;
}
1,3 Top
my output is:
time_overloaded_operators.cpp: In member function ‘Time& Time::operator+=(const Time&)’:
time_overloaded_operators.cpp:75: error: passing ‘const Time’ as ‘this’ argument of ‘unsigned int Time::TotalTimeInSeconds()’ discards qualifiers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是
TotalTimeInSeconds
被声明为禁止通过 const 引用调用它,但随后operator+=
尝试在time
上调用它,这是一个常量引用。解决方法是声明 unsigned int TotalTimeInSeconds() const; ,它表示可以通过 const 引用(以及通过 const 指针和 const 对象的名称)调用成员函数。
The problem is that
TotalTimeInSeconds
is declared so as to forbid calling it via a const reference, but thenoperator+=
tries to call it ontime
, which is a const reference.The fix is to declare
unsigned int TotalTimeInSeconds() const;
, which says that the member function can be called via const references (and via const pointers, and the names of const objects).更改:
to
因此也:
to
这基本上是说
Time
对象不会通过调用TotalTimeInSeconds
进行修改,因此const Time
> 对象可以传递给operator+=
。否则,无法确定在operator+=
内部调用TotalTimeInSeconds
时Time
对象不会被修改,因此>const
限定符被丢弃。这基本上就是错误的含义。Change:
to
And therefore also:
to
Which is basically saying that the
Time
object won't be modified by callingTotalTimeInSeconds
, and so thereforeconst Time
objects can be passed tooperator+=
. Otherwise it can't be sure that theTime
object won't be modified when callingTotalTimeInSeconds
inside ofoperator+=
, and therefore theconst
qualifier is discarded. That's basically what the error means.