丢弃限定符

发布于 2024-12-19 15:48:15 字数 2387 浏览 3 评论 0原文

我不太确定我的问题是什么。它说“放弃限定符”。我不知道为什么会发生这种情况。如果我删除重载运算符+=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 技术交流群。

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

发布评论

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

评论(2

东风软 2024-12-26 15:48:15

问题是 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 then operator+= tries to call it on time, 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).

盛装女皇 2024-12-26 15:48:15

更改:

unsigned int TotalTimeInSeconds();

to

unsigned int TotalTimeInSeconds() const;

因此也:

unsigned int Time::TotalTimeInSeconds() {

to

unsigned int Time::TotalTimeInSeconds() const {

这基本上是说 Time 对象不会通过调用 TotalTimeInSeconds 进行修改,因此 const Time > 对象可以传递给operator+=。否则,无法确定在 operator+= 内部调用 TotalTimeInSecondsTime 对象不会被修改,因此 >const 限定符被丢弃。这基本上就是错误的含义。

Change:

unsigned int TotalTimeInSeconds();

to

unsigned int TotalTimeInSeconds() const;

And therefore also:

unsigned int Time::TotalTimeInSeconds() {

to

unsigned int Time::TotalTimeInSeconds() const {

Which is basically saying that the Time object won't be modified by calling TotalTimeInSeconds, and so therefore const Time objects can be passed to operator+=. Otherwise it can't be sure that the Time object won't be modified when calling TotalTimeInSeconds inside of operator+=, and therefore the const qualifier is discarded. That's basically what the error means.

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