抛弃预选赛 [英] discards qualifiers

查看:53
本文介绍了抛弃预选赛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我的问题是什么.它说放弃预选赛".我不知道为什么会这样.如果我为重载的运算符 + = 删除 const ,一切都很好.有人可以帮我吗?

 //关于在Time变量上重载运算符的实践#include< iostream>使用命名空间std;上课时间 {上市://构造函数时间(const unsigned int& day = 0,const unsigned int& hour = 0,const unsigned int&分钟= 0,const unsigned int&秒= 0):day_(day),hour_(小时),分钟_(分钟),second_(second){}时间(const unsigned int& second = 0):day_(0),hour_(0),分钟_(0),second_(second){}//复制构造函数时间(常量时间和时间):day_(time.day_),hour_(time.hour_),分钟_(时间.分钟_),second_(time.second_){}//赋值运算符时间与时间运算符=(const Time& time){day_ = time.day_;hour_ = time.hour_;minutes_ = time.minute_;second_ = time.second_;返回* this;}//析构函数〜Time(){}//重载运算符时间与时间运算符+ =(const Time& time);时间运算符+(const Time& time);私人的:unsigned int day_;unsigned int hour_;unsigned int minutes_;unsigned int second_;void ConvertSecondsToTime();unsigned int TotalTimeInSeconds();};//主功能int main(){返回0;}//重载运算符unsigned int Time :: TotalTimeInSeconds(){返回(天_ * 24 * 60 * 60 +小时_ * 60 * 60 +分钟_ * 60 +第二_);}无效的时间:: ConvertSecondsToTime(){而(second_> = 60){second_-= 60;minutes_ + = 1;}一会儿(minute_> = 60){分钟--= 60;hour_ + = 1;}一会儿(hour_> = 24){hour_-= 24;day_ + = 1;}}时间与时间时间:: operator + =(常量时间和时间){second_ = this-> TotalTimeInSeconds()+ time.TotalTimeInSeconds();ConvertSecondsToTime();返回* this;}时间Time :: operator +(const Time& time){时间temp(* this);temp + = time;返回温度}1,3顶部 

我的输出是:

  time_overloaded_operators.cpp:在成员函数"Time&时间:: operator + =(const Time&)’:time_overloaded_operators.cpp:75:错误:将"const Time"作为"unsigned int Time :: TotalTimeInSeconds()"的"this"参数传递 

解决方案

问题是声明了 TotalTimeInSeconds 以便禁止通过const引用对其进行调用,但随后 operator + = 尝试在 time 上调用它,这是一个const引用.

解决方法是声明 unsigned int TotalTimeInSeconds()const; ,它表示可以通过const引用(以及const指针和const对象的名称)调用成员函数./p>

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

解决方案

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).

这篇关于抛弃预选赛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆