【问题标题】:discards qualifiers丢弃限定符
【发布时间】:2011-12-04 03:21:41
【问题描述】:

我不确定我的问题是什么。它说“丢弃限定符”。我不知道为什么会这样。如果我删除 const 的重载 operator+= 一切都很好。有人可以帮帮我吗?

// 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

【问题讨论】:

  • ¤ 制作纯存取函数const。例如,制作TotalTimeInSeconds() const。干杯&hth.,
  • 真的@Alf,你应该回去回答。
  • @Xeo 但是到一些技术性更强的网站...

标签: c++ g++


【解决方案1】:

问题在于 TotalTimeInSeconds 被声明为禁止通过 const 引用调用它,但随后 operator+= 尝试在 time 上调用它,这是一个 const 引用。

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

【讨论】:

  • 非const成员可以调用TotalTimeInSeconds()吗?
  • @ihm:我不确定这是否回答了你的问题,我不知道你的意思,但是添加const 意味着可以通过const 引用调用成员函数以及 通过非常量引用,而不是代替。 time.TotalTimeInSeconds() 变为合法,this-&gt;TotalTimeInSeconds() 仍然合法。
【解决方案2】:

变化:

unsigned int TotalTimeInSeconds();

unsigned int TotalTimeInSeconds() const;

因此还有:

unsigned int Time::TotalTimeInSeconds() {

unsigned int Time::TotalTimeInSeconds() const {

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

【讨论】:

  • 非 const 成员可以在使其变为 const 后调用 TotalTimeInSeconds() 吗?
  • @ihm: 是的,就像非const 变量可以通过指针等const 参数传递给函数一样,只是表明它不会被修改。
猜你喜欢
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多