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

  • Home
  • SEARCH
  • 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 6637585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:19:58+00:00 2026-05-25T23:19:58+00:00

I don’t know if this question is already answered on stackoverflow. But I simply

  • 0

I don’t know if this question is already answered on stackoverflow. But I simply can not find the right keyword to search for.

I inserted some stripped down version of my code below.

So basically what I’m trying to do in my main(), is to subtract 122 from t2. I suppose my compiler converts the integer to a Timestamp object automatically and then subtracts it as showed in ‘Timestamp.cpp’.

But when it arrives at t4 it doesn’t convert it and give me the following error:

no match for ‘operator-‘ in ‘722 – t1’

I’m 100% sure that it is possible. But how?

Maybe I’m totally wrong about converting… So please do hesitate to correct me, I’m trying to learn something.

STRIPPED DOWN CODE:

main.cpp:

#include <iostream>
#include <iomanip>

#include "Timestamp.h"

using namespace std;

int main() {
    Timestamp t3(t2 - 122);
    cout << "T3 = " << t3 << endl;
    Timestamp t4(722 - t1);
    cout << "T4 = " << t4 << endl;

    return 0;
}

Timestamp.h

#ifndef TIJDSDUUR_H
#define TIJDSDUUR_H

using namespace std;

class Timestamp {
    public:
        Timestamp(int);
        Timestamp operator- (const Timestamp &t);
    private:
        int hour;
        int min;
};

Timestamp.cpp

Timestamp::Timestamp(int m) : hour(0), min(m) {

}

Timestamp Timestamp::operator- (const Timestamp &t) {
    Timestamp temp;

    temp.hour = hour;
    temp.min = min;

    temp.hour -= t.hour;
    temp.min -= t.min;

    while(temp.min < 0.00) {
        temp.hour--;
        temp.min += 60;
    }

    return temp;
}
  • 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-25T23:19:59+00:00Added an answer on May 25, 2026 at 11:19 pm

    Contrary to what the other answers propose, you do not need to provide an specialized operator- that takes an int and a Timestamp, rather you can (and probably should) make operator- a non-member function:

    Timestamp operator-( Timestamp lhs, Timestamp const & rhs ); // Returns a timestamp?? really??
    

    That way the compiler is free to apply conversions to both the left hand side and the right hand side operands, and use the implicit conversion from int to Timestamp.

    You can read a short description on design and implementation of operator overloads here, or you can search in the [C++-faq] tag in SO for “operator overload”.

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

Sidebar

Related Questions

Don't know why but I can't find a solution to this. I have 3
Don't know if this is the right place to ask this, but I will
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
Don't know if this is an eclipse specific problem but whenever I declare a
Don't know if this has been answered before. Have custom routes to users. If
I don't know if this has been asked before, but what i'd like to
Don't ask me why but i can't use this method because I need to
Don't need to do this right now but thinking about the future... What would
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query

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.