Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6891061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:21:42+00:00 2026-05-27T06:21:42+00:00

I am not really sure what my problem is. it says discards qualifier. I

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:21:43+00:00Added an answer on May 27, 2026 at 6:21 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this is possible but I'm not really sure where to start. Has
Question says it all, really. I'm not sure what the problem is. I'm fairly
I'm not really sure if there is a single problem here or I have
So i'm not really sure why this is happening but I'm running through some
I'm not really sure how to google this one . I would like to
I'm not really sure how to title this question but basically I have an
Sorry, I'm not really sure of the right way to ask this one so
I feel like there's a simple solution to this, but I'm not really sure
Not really sure if this is possible, but I am running this on Terminal:
Not sure if this really belongs here, so please move it if it doesn't.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.