 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthew Hurne Guest
|
Posted: Sat Dec 25, 2004 10:10 am Post subject: Overloaded operator+ not returning value that overloaded ope |
|
|
I am working on a Time class which stores the time in 24-hour format as
HH:MM:SS (internally as integers). I'm just doing it for the sake of
learning really. Regardless, I have overloaded += and +, and << as
such:
****Time.h:****
class Time {
public:
....
Time& operator+=(const Time& rhs);
....
};
Time operator+(const Time& lhs, const Time& rhs);
std::ostream& operator<<(std::ostream&, Time&);
****Time.cpp****
Time& Time::operator+=(const Time& rhs)
{
this->seconds += rhs.seconds;
while (this->seconds >= 60) {
this->seconds -= 60;
this->minutes++;
}
this->minutes += rhs.minutes;
while (this->minutes >= 60) {
this->minutes -= 60;
this->hours++;
}
this->hours += rhs.hours;
while (this->hours > 23) {
this->hours -= 23;
}
return *this;
}
Time operator+(const Time& lhs, const Time& rhs)
{
Time ret = lhs;
ret += rhs;
return ret;
}
std::ostream& operator<<(std::ostream& os, Time& time)
{
std::ostringstream ostr;
if(time.get_hours() < 10) {
ostr << '0';
}
ostr << time.get_hours() << ':';
if(time.get_minutes() < 10) {
ostr << '0';
}
ostr << time.get_minutes() << ':';
if(time.get_seconds() < 10) {
ostr << '0';
}
ostr << time.get_seconds();
return os << ostr.str();
}
Now, in my test driver program, the following lines of code work fine:
Time time5 = time2 + time3;
cout << "time2 + time3 = " << time5 << endl;
But, this line of code causes compiler errors:
cout << time2 + time3 << endl; //NOT WORKING!
Here is an excerpt of the error:
TimeDriver.cpp: In function `int main(int, char**)':
TimeDriver.cpp:22: error: no match for 'operator<<' in 'std::cout <<
operator+(const Time&, const Time&)(((const Time&)((const
Time*)(&time3))))'
/usr/lib/gcc/i386-redhat-linux/3.4.2/../../../../include/c++/3.4.2/bits/ostream.tcc:63:
note: candidates are: std::basic_ostream<_CharT, _Traits>&
std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT,
_Traits>&(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT =
char, _Traits = std::char_traits<char>]
It goes on with more, but I'm assuming this is the most important part.
I was thinking maybe << should be std::ostream&
operator<<(std::ostream&, Time) - Time as pass-by-value rather than
pass-by-reference, but I still get compile errors. I can't for the
life of me figure out what's wrong. I was thinking maybe it has
something to do with operator+ returning a Time...but I figured that
would work ok because it is returning by value and not by reference.
Any help with this would be greatly appreciated! Thanks...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Dec 25, 2004 5:16 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
Peanut oil for frying.
Pound the cutlets.
Dredge in flour, eggs, then the bread crumb mixture.
Fry till golden brown in 350° peanut oil.
In a baking pan, place a layer of gravy,
then one of meat, gravy, and cheese.
Another layer each of meat, gravy, and cheese.
Then bake at 350° for 45 minutes.
Serve on hot pasta with romano cheese.
Southern Fried Small-fry
Tastes like fried chicken, which works just as well.
In fact you may want to practice cutting up whole chickens
for frying before you go for the real thing.
Whole chicken is much more efficient and inexpensive than buying pieces.
1 tiny human, cut into pieces
2 cups flour
Onion, garlic
Salt
pepper
garlic powder
cayenne pepper
hot sauce, etc.
Oil for frying
Mix milk, eggs, hot sauce in a bowl, add chopped onion and garlic.
Season the meat liberally, and marinate for several hours.
Place seasoned flour in a paper or plastic shopping bag,
drop pieces in a few a time, shake to coat thoroughly,
then deep fry in hot oil (350°) for about 15 minutes.
Drain and place on paper towels.
Miscarriage with Mustard Greens
Why waste it? Otherwise, and in general, use ham or salt pork to season greens.
The technique of smothering greens can be used with many vegetables;
green beans work especially well. Meat is not necessary every day, don?t
be afraid to alter any dish to vegetarian tastes.
1 premature baby, born dead
Large bunch of mustard greens
2 white onions, 1 cup cho
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Dec 25, 2004 5:48 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
minced garlic
4-6 ounces bamboo shoots
Sherry
chicken broth
oil for deep frying (1 gallon)
Salt
pepper
soy & teriyaki
minced ginger, etc.
1 tablespoon cornstarch dissolved in a little cold water
1 egg beaten
Make the stuffing:
Marinate the flesh in a mixture of soy and teriyaki sauces
then stir fry in hot oil for till brown - about 1 minute, remove.
Stir-fry the vegetables.
Put the meat back into the wok and adjust the seasoning.
De-glaze with sherry, cooking off the alcohol.
Add broth (optional) cook a few more minutes.
Add the cornstarch, cook a few minutes till thick,
then place the stuffing into a colander and cool;
2 hours
Wrap the rolls:
Place 3 tablespoons of stuffing in the wrap, roll tightly -
corner nearest you first, fold 2 side corners in,
wrap till remaining corner is left.
Brush with egg, seal, and allow to sit on the seal for
a few minutes.
Fry the rolls:
325° if using egg roll wraps, 350° for spring roll wraps.
Deep fry in peanut oil till crispy golden brown, drain on paper towels.
Lemon Neonate
Turkey serves just as well, and in fact even looks a bit like a
well-dressed
|
|
| Back to top |
|
 |
Matthew Hurne Guest
|
Posted: Sat Dec 25, 2004 7:40 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
skillet, brown the meat in a little olive oil,
then add onions, peppers, and celery (all chopped finely)
and season well.
Place in a large bowl and cool.
Add seasoned breadcrumbs and a little of the tomato gravy,
enough to make the mixture pliable.
Divide the stuffing among the cabbage leaves then roll.
Place seam down in a baking pan.
Ladle tomato gravy on top,
and bake at 325° for 30 - 45 minutes.
Umbilical Cordon Bleu
Nothing is so beautiful as the bond between mother and child,
so why not consume it?
Children or chicken breasts will work wonderfully also.
4 whole umbilical chords (or baby breasts, or chicken breasts)
4 thin slices of smoked ham, and Gruyere cheese
Flour
eggwash (milk and eggs)
seasoned bread crumbs
1 onion
minced
salt
pepper
butter
olive oil
Pound the breasts flat (parboil first if using umbilical
cords so they won?t be tough).
Place a slice of ham and cheese on each, along with some minced onion
then fold in half, trimming neatly.
Dredge in flour, eggwash, then seasoned breadcrumbs;
allow to sit for a few minutes.
Sauté in butter and olive oil until golden brown,
about 6 minutes on each side.
Shish Kababes
As old as the hills, this technique has employed seafood, beef, pork, lamb,
poultry, and vegetables; just about any
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Dec 25, 2004 8:34 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
"Matthew Hurne" <matthew.hurne (AT) gmail (DOT) com> writes:
| Quote: | std::ostream& operator<<(std::ostream&, Time&);
|
Writing a Time object to a stream is not supposed to modify the Time
object. You should therefore change type of the second argument to
Time const &.
| Quote: | std::ostream& operator<<(std::ostream& os, Time& time)
{
std::ostringstream ostr;
|
Side note: Why don't you write to os directly?
| Quote: | Now, in my test driver program, the following lines of code work fine:
Time time5 = time2 + time3;
cout << "time2 + time3 = " << time5 << endl;
But, this line of code causes compiler errors:
cout << time2 + time3 << endl; //NOT WORKING!
|
The expression time2+time3 evaluates to a temporary object of type
Time. When invoking operator<<, a reference to Time (non-const!) is
attempted to be bound to this temporary object.
C++ does not allow a reference to non-const to be bound to a temporary
object. The reason is that even if operator<< did modify its Time
parameter through its reference to non-const, it would be hard to tell
because the modified object is temporary, i.e. it is destroyed at the
end of the full-expression.
Binding a reference to non-const is thus typically an oversight, and
the C++ Standardization committee felt this type of oversight did
enough harm that banning it is justified.
If you change the second parameter type of operator<< as I wrote
above, your code will compile.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Dec 25, 2004 8:39 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
Matthew Hurne wrote:
| Quote: | Time operator+(const Time& lhs, const Time& rhs);
std::ostream& operator<<(std::ostream&, Time&);
|
operator+ returns an rvalue (a temporary). You can't
bind that to a reference to non-const object.
You need to define operator to take either "Time" (by value)
or "const Time&" (reference to const, which can be initialized
with an rvalue).
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Sat Dec 25, 2004 9:21 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
flour in a paper or plastic shopping bag,
drop pieces in a few a time, shake to coat thoroughly,
then deep fry in hot oil (350°) for about 15 minutes.
Drain and place on paper towels.
Miscarriage with Mustard Greens
Why waste it? Otherwise, and in general, use ham or salt pork to season greens.
The technique of smothering greens can be used with many vegetables;
green beans work especially well. Meat is not necessary every day, don?t
be afraid to alter any dish to vegetarian tastes.
1 premature baby, born dead
Large bunch of mustard greens
2 white onions, 1 cup chopped celery
Vegetable oil (or hog fat)
Salt, pepper, garlic, etc.
Lightly brown onions, celery, garlic and meat in large heavy pot.
Add a little water and the greens (which should be thoroughly cleaned and washed).
Smother slowly for at least 2 hours, adding small amounts of water
when it starts to stick.
Stir frequently.
When ready - serve with rice, grilled smoked sausage, green salad, and iced tea.
Coffee and apple pie then brandy.
Maternity Ward Pot Luck Dinner
If you can?t get anything fresh from the hospital, nursery, or morgue;
you can at least get rid of all the leftovers in your refrigerator.
1 - 2 lbs. cubed meat (human flesh, chicken, turkey, beef...)
1 -2 lbs. coarsely chopped vegetables
(carrots, potatoes, turnips, cauliflower, cabbage...)
Bell pepper
onions
garlic
ginger
salt pepper, etc.
Olive oil
butter
Brown the meat and some chopped onions, peppers, and garilic in olive oil,
place in baking dish, layer with vegetables seasoning and butter.
Bake at 325° for 30 - 45 minutes.
Serve with hot dinner rolls, fruit salad and sparkling water.
Bébé Buffet 1
Show off with whole roasted children replete with apples in mouths -
and babies? heads stuffed with wild rice. Or keep it simple with a
hearty main course such as stew
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sat Dec 25, 2004 9:23 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
alive!
You don?t need silverware, the hot spicy meat comes off in your hands.
6 live babies
1 lb. smoked sausage
4 lemons
whole garlic
2 lb. new potatoes
4 ears corn
1 box salt
crab boil
Bring 3 gallons of water to a boil.
Add sausage, salt, crab boil, lemons and garlic.
Drop potatoes in, boil for 4 minutes.
Corn is added next, boil an additional 11 minutes.
Put the live babies into the boiling water and cover.
Boil till meat comes off easily with a fork.
Oven-Baked Baby-Back Ribs
Beef ribs or pork ribs can be used in this recipe,
and that is exactly what your dinner guests will assume!
An excellent way to expose the uninitiated to this highly misunderstood
yet succulent source of protein.
2 human baby rib racks
3 cups barbecue sauce or honey glaze (see index)
Salt
black pepper
white pepper
paprika
Remove the silverskin by loosening from the edges,
then stripping off.
Season generously, rubbing the mixture into the baby?s flesh.
Place 1 quart water in a baking pan, the meat on a wire rack.
Bake uncovered in 250° oven for 1˝ hours.
When browned, remove and glaze,
return to oven and bake 20 minutes more to form a glaze.
Cut ribs into i
|
|
| Back to top |
|
 |
Matthew Hurne Guest
|
Posted: Sat Dec 25, 2004 10:29 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
Its free, you can sell the crib, baby clothes, toys, stroller... and so easy to
procure if such a lucky find is at hand (just pick him up from the crib and
he?s good to go)!
SIDS victim, cleaned
˝ cup cooking oil
Carrots
onions
broccoli
whole cabbage
fresh green beans
potato
turnip
celery
tomato
˝ stick butter
1 cup cooked pasta (macaroni, shells, etc.)
Remove as much meat as possible, cube, and brown in hot oil.
Add a little water, season, then add the carcass.
Simmer for half an hour keeping the stock thick.
Remove the carcass and add the vegetables slowly to the stock,
so that it remains boiling the whole time.
Cover the pot and simmer till vegetables are tender
(2 hours approximately).
Continue seasoning to taste.
Before serving, add butter and pasta,
serve piping with hot bread and butter.
Offspring Rolls
Similar to Vietnamese style fried rolls, they have lots of meat
(of course this can consist of chicken, beef, pork, or shrimp).
Who can resist this classic appetizer; or light lunch served with
a fresh salad? Versatility is probably this recipe?s greatest virtue,
as one can use the best part of a prime, rare, yearling, or the
morticians occasional horror: a small miracle stopped short by a
drunk driver, or the innocent victim of a drive-by shooting...
2 cups finely chopped very young human flesh
1 cup shredded cabbage
1 cup bean sprouts
5 sprigs green onion, finely chopped
5 cloves minced garlic
4-6 ounces bamboo shoots
Sherry
chicken broth
oil for deep frying (1 gallon)
Salt
pepper
soy & teriyaki
minced ginger, etc.
1 tablespoon cornstarch dissolved in a little cold water
1 egg beaten
Make the stuffing:
Marinate the flesh in a mixture of soy and teriyaki sauces
then stir fry in hot oil for till brown - about 1 minute, remove.
Stir-fry the vegetables.
Put the meat back into the
|
|
| Back to top |
|
 |
Joshua Lehrer Guest
|
Posted: Sun Dec 26, 2004 10:18 am Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
| Quote: | class Time {
public:
...
Time& operator+=(const Time& rhs);
...
};
Time operator+(const Time& lhs, const Time& rhs);
std::ostream& operator<<(std::ostream&, Time&);
|
std::ostream& operator<<(std::ostream&, CONST Time&);
cout << t1+t2;
t1+t2 is a temporary, and therefore can only be bound to a
const-reference.
joshua lehrer
factset research systems
NYSE:FDS
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Dec 26, 2004 10:51 am Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
"Matthew Hurne" <matthew.hurne (AT) gmail (DOT) com> wrote...
| Quote: | I am working on a Time class which stores the time in 24-hour format as
HH:MM:SS (internally as integers). I'm just doing it for the sake of
learning really. Regardless, I have overloaded += and +, and << as
such:
****Time.h:****
class Time {
public:
...
Time& operator+=(const Time& rhs);
...
};
Time operator+(const Time& lhs, const Time& rhs);
std::ostream& operator<<(std::ostream&, Time&);
|
Make this
std::ostream& operator<<(std::ostream&, Time const &);
and you're all set.
V
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthew Hurne Guest
|
Posted: Sun Dec 26, 2004 6:56 pm Post subject: Re: Overloaded operator+ not returning value that overloaded |
|
|
Thanks to all, it works and I think I understand the reasoning behind
it. I wouldn't have been able to find that answer on my own - nothing
on the C++ Faq Lite that I could see and the index of my copy of The
C++ Programming Language didn't point me to anything of use either.
Your responses were a welcome Christmas present yesterday. :-)
Matt
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|