3 Replies - 260 Views - Last Post: 05 March 2018 - 03:45 PM Rate Topic: -----

#1 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-October 17

Newbie to operator overloading

Posted 28 February 2018 - 11:34 PM

Not quite sure what I'm messing up, would really appreciate some help. Not allowed to change the code from main. Thanks!

#include<iostream>
using namespace std;

//Overload the necessary operators to be able to run the main() function below :

class CLOCK
{
public:
	int hour = 0, minute = 0, seconds = 0;

	CLOCK() {
	};

	CLOCK(int h, int m, int s)
	{
		hour = h;
		minute = m;
		seconds = s;
	}

	CLOCK operator+ (CLOCK& c) {
		CLOCK clock;
		clock.hour = this->hour + c.hour;
		clock.minute = this->minute + c.minute;
		clock.seconds = this->seconds + c.seconds;
		return clock;
	}

	CLOCK operator++ (int) {
		CLOCK clock;
		hour++;
		minute++;
		seconds++;
		return clock;
	}
};

void main()
{

	CLOCK c(10, 10, 10);

	cout << c << endl;

	c = c + 10; // should display 10 minutes to my clock

	cout << c.hour << c.minute << c.seconds << endl;  // should display 102010

	c++; // this should increment hours, time now is 012010

}



Is This A Good Question/Topic? 0
  • +

Replies To: Newbie to operator overloading

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 825
  • View blog
  • Posts: 3,337
  • Joined: 13-June 14

Re: Newbie to operator overloading

Posted 01 March 2018 - 02:25 AM

For a start, you haven't even overloaded <<, so your program shouldn't compile, giving you an error about that. Please remember to post all error messages here, in their entirety, since they give important information about what the problem is.
Was This Post Helpful? 0
  • +
  • -

#3 CooolGuy   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-October 17

Re: Newbie to operator overloading

Posted 04 March 2018 - 08:10 PM

Sorry for taking so long to reply. Got caught up with things. My bad for not sharing the errors. Here ya go.

(no operator "<<" matches these operands)
(no operator "+" matches these operands)
(binary '<<': no operator found which takes a right-hand operand of type 'CLOCK' (or there is no acceptable conversion))
(binary '+': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion))

This post has been edited by ndc85430: 09 March 2018 - 11:08 PM
Reason for edit:: Removed quote of previous post.

Was This Post Helpful? 0
  • +
  • -

#4 tony jay   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 50
  • Joined: 31-January 18

Re: Newbie to operator overloading

Posted 05 March 2018 - 03:45 PM

All those error messages ultimately devolve to just two problems in the code in main:

* on line 43 you are asking cout to output the object c, which is of type CLOCK, you have not provided an overload for operator<< that will allow this!;
* secondly, just below it on line 45 you are asking the code to add an int to an object of type CLOCK. Again there is no operator overload to allow this. The overload that you have provided is designed to add only objects of type CLOCK together.

If you have got far enough to write code for two operator overloads it should not take too much more research to figure out how to write operator overloads that actually allow the code in main() to run.
And with the correct overloads that code will run as desired.
When you get this right you will experience a discernible sense of satisfaction!

Good luck!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1