5 Replies - 610 Views - Last Post: 14 March 2009 - 02:07 PM Rate Topic: -----

#1 luckyman76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

overloading operators as friends

Posted 14 March 2009 - 01:02 PM

I'm overloading operators to go with a Timer class...the timer's capacity is 99:59:59, and then it starts over and an overflow flag is set. So, in my code, when my hours are greater than 99, I try to increment overflow and an error comes back, which says, "_overflow undeclared, first use this function."
I have written this as a friend to the class so I should be able to access it's members, right? I was able to do so for the _hr, _sec, _min....These three are ints, while _overflow is a boolean expression....though my member methods of my class are able to use it and increment it without a problem...
What am I doing wrong and how can I increment my overflow?

Here's the code:
const Timer operator +(const Timer& time1, const Timer& time2)
{
   int sec = time1._sec + time2._sec;
   int min = time1._min + time2._min;
   int hr = time1._hr + time2._hr;

   if (sec > 59) {
	 sec -= 60;
	 min++;
   }
   if (min > 59) {
	 min -= 60;
	 hr++;
   }
   if (hr > 99) {
	 hr -= 100;
	 _overflow = _overflow + 1;  // Here's where I'm getting the error...
   }

   return Timer(hr, min, sec);
}



Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: overloading operators as friends

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: overloading operators as friends

Posted 14 March 2009 - 01:41 PM

Aside from anything else, why are you trying to increment a boolean? :crazy:
Was This Post Helpful? 0
  • +
  • -

#3 luckyman76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: overloading operators as friends

Posted 14 March 2009 - 01:45 PM

I know...it doesn't make sense...duh on my part....

but even when I would try to say _overflow = 1;

which other member methods that were written for us to include do, it does not work...

View PostKYA, on 14 Mar, 2009 - 12:41 PM, said:

Aside from anything else, why are you trying to increment a boolean? :crazy:

Was This Post Helpful? 0
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: overloading operators as friends

Posted 14 March 2009 - 01:51 PM

The whole point of a boolean is to use true/false. Otherwise you just might as well use a bit field. In order to solve your problem I need to see your class file. Maybe you misspelled something? For some data fields to work and one to not to doesn't make sense.


edit: is _overflow coming from the calling object?

You need to use a setter or accessor object to access _overflow then.

This post has been edited by KYA: 14 March 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#5 sparkart  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 113
  • View blog
  • Posts: 690
  • Joined: 16-February 09

Re: overloading operators as friends

Posted 14 March 2009 - 01:53 PM

Is that supposed to be the Timer class's member or the friend's member?
_overflow = _overflow + 1;


If it is the friend's member, it's because you're looking for it in the wrong scope, should look something like this: friend._overflow

You need an object to access it.

_overflow assumes that it is in the Timer class's scope.

This post has been edited by e_barroga: 14 March 2009 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#6 luckyman76  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 05-February 09

Re: overloading operators as friends

Posted 14 March 2009 - 02:07 PM

That was it...I created a class member to access the _overflow..
Thanks!


View Poste_barroga, on 14 Mar, 2009 - 12:53 PM, said:

Is that supposed to be the Timer class's member or the friend's member?
_overflow = _overflow + 1;


If it is the friend's member, it's because you're looking for it in the wrong scope, should look something like this: friend._overflow

You need an object to access it.

_overflow assumes that it is in the Timer class's scope.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1