问题
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
回答1:
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).
回答2:
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.
来源:https://stackoverflow.com/questions/8373010/discards-qualifiers