need help on some c++ hw

E

Enc5

data structures teacher gave me like 50 questions, I have a few I'm not
sure about. My last c++ class was like 8 months ago and I can't
remember a lot of things. If anyone could help me on any of the
following questions, it'd be great.





Find the errors in the follwing code:



-First one

class mystery //line 1

{

...

bool operator <= (mystery); //line 2

...

};



bool mystery::<=(mystery rightObj) //line 3

{

...

}



-Second one

class mystery //line 1

{

...

friend operator+ (mystery); //line 2

//Overload the binary operator +

...

};





And the last ones are:



How many parameters are required to overload the preincrement operator
for a class as a friend function?



How many parameters are required to overload the postincrement operator
for a class as a member function?



Any help would be appreciated.
 
L

lallous

It has been a while since I haven't used c++ operator overloading, however:
-First one

class mystery //line 1

{

...

bool operator <= (mystery); //line 2

...

};



bool mystery::<=(mystery rightObj) //line 3
change to:
bool mystery::eek:perator <=(mystery rightObj) //line 3

-Second one

class mystery //line 1

{

...

friend operator+ (mystery); //line 2
change to:
friend operator+ (mystery, mystery); //line 2
//Overload the binary operator +

...

};



And the last ones are:
How many parameters are required to overload the preincrement operator
for a class as a friend function? Two.


How many parameters are required to overload the postincrement operator
for a class as a member function?
Two.


Would be glad if someone corrects me in case of mistakes.
 
G

Gianni Mariani

lallous said:
It has been a while since I haven't used c++ operator overloading, however:
....

change to:
friend operator+ (mystery, mystery); //line 2

I don't think so.

I don't think so.

To the OP. You can easily find out if you use a compiler to check it out.

If you demonstrate some actual work in attempting to solve your problem,
you will get far greater levels of support from this NG - even if it's
homework.

If a job candidate could not answer these questions and this is all they
showed, the interview would be awfully short.
 
W

WW

Gianni said:
WW wrote:
...

Please don't feed the TROLLS.

OK, sorry. I did not realize you were trolling:

"To utter a posting on Usenet designed to attract predictable responses or
flames; or, the post itself."

But as I read it again (stating something is wrong without telling what it
is therefore not helping really but attracting attention for possible follow
up posts with contents wondering about the necessity of yours) you really
have been trolling.
 
J

Jerry Coffin


He said he didn't think so -- given an inability to read minds, about
the closest thing there is to proof is the fact that he's said so, and
he doesn't seem to have any reason to lie about it.

Now, as to why he might think that: because it's a BAD idea. It'll
normally _work_ as it is, but will often be substantially less efficient
than it could be. As a general rule, you're better off with your
parameters as constant references than making them objects. The latter
requires that the objects be copied, which should normally be avoided
unless copying these objects is known to be cheap. No information to
that affect has been provided, and the name "mystery" seems intended to
indicate that we know relatively little about the class in question.

Much as above.

However, in this case there's no question that his thinking is
absolutely correct. If the non-member overload of operator++ takes two
parameters, it'll be a post-increment rather than a pre-increment (see
$13.5.7 if you want proof of that).

Unlike many operators, however, ++ and -- should normally be overloaded
as member functions. The usual reason for making operators global is to
support implicit conversions on the left operand. Since ++ and --
normally modify the object to which they're applied, supporting implicit
conversions is usually undesirable.
 
W

WW

Jerry said:
[SNIP]
Now, as to why he might think that: because it's a BAD idea. It'll
normally _work_ as it is, but will often be substantially less
efficient than it could be. As a general rule, you're better off
with your parameters as constant references than making them objects.
The latter requires that the objects be copied, which should normally
be avoided unless copying these objects is known to be cheap. No
information to that affect has been provided, and the name "mystery"
seems intended to indicate that we know relatively little about the
class in question.

This is exactly the thing I was missing. The reply was arrogant. The
person why has tried to help was (I am sure) not deliberately misleading.
He did not deserve that kind of tone. And telling what is wrong is why we
are all here. Not to show up with arrogant, empty statements.
[SNIP]
However, in this case there's no question that his thinking is
absolutely correct.

I know. He could have explained it. If he does not want to help he can
save all of us the time. If he wants, he can do it as you did.
If the non-member overload of operator++ takes
two parameters, it'll be a post-increment rather than a pre-increment
(see $13.5.7 if you want proof of that).

Eggzakt lee. I would not have required a reference to the standard in the
post I claim to be arrogant, but a simple description.
Unlike many operators, however, ++ and -- should normally be
overloaded as member functions. The usual reason for making
operators global is to support implicit conversions on the left
operand. Since ++ and -- normally modify the object to which they're
applied, supporting implicit conversions is usually undesirable.

Certainly. That's why I have absolutely forgot to write the non-member
version into my answer. Which is of course my mistake - but I actually do
not like to put things outside if I can put them inside - so I have never
ever used the non-member form.
 
G

Gianni Mariani

Jerry said:
He said he didn't think so -- given an inability to read minds, about
the closest thing there is to proof is the fact that he's said so, and
he doesn't seem to have any reason to lie about it.

Now, as to why he might think that: because it's a BAD idea. It'll
normally _work_ as it is, but will often be substantially less efficient
than it could be. As a general rule, you're better off with your
parameters as constant references than making them objects. The latter
requires that the objects be copied, which should normally be avoided
unless copying these objects is known to be cheap. No information to
that affect has been provided, and the name "mystery" seems intended to
indicate that we know relatively little about the class in question.

They are all really good points. But my issue is this:

error: ISO C++ forbids declaration of `operator+' with no type

It took 30 seconds to get this out of a compiler. It takes 5 minutes to
post and possibly hours to get a response.

The reason why I did not answer the questions were because this has to
do with school work and the original poster did not demonstrate even
tring to compile the code to see what errors there were. Had I seen
even a small attempt by the OP at solving the problems I would have
answered the questions.

As to why I responded at all. Apart from not helping people who don't
help themselves, I don't believe you should let anyone be misinformed,
even slightly. So, I felt obliged to at least hint that there was an
issue with the answers. If anyone read any *TONE* in the response,
you're horribly mistaken.

As for WW, I think he has an axe to grind and is simply trolling. He's
obviously smart enough to comprehend why I wrote what I did and smart
enough to have figured out the errors for himself. It's better not to
respond. Hence my plea for everyone to "Please don't feed the ...".
He'll get over it soon enough.
 
W

WW

Gianni said:
They are all really good points. But my issue is this:

error: ISO C++ forbids declaration of `operator+' with no type

It took 30 seconds to get this out of a compiler. It takes 5 minutes
to post and possibly hours to get a response.

Apparently you have a compiler. The OP might not have one.

[SNIP accusation of the OP to be lazy]
As to why I responded at all. Apart from not helping people who don't
help themselves, I don't believe you should let anyone be misinformed,
even slightly.

Better to leave them uninformed and bash them without knowing what they
could or could nto do.
So, I felt obliged to at least hint that there was an
issue with the answers. If anyone read any *TONE* in the response,
you're horribly mistaken.

Arrogant. That is the tone.
As for WW, I think he has an axe to grind and is simply trolling.

You are arrogant again. Stop calling me troll. I do not appreciate it.
You have posted an absolutely empty (therefore confusing to the OP),
unhelpful, arrogant post. I haver asked for proof. As you can see: it can
be done.
He's obviously smart enough to comprehend why I wrote what I did and
smart enough to have figured out the errors for himself.

And I am smart enough to figure out your silly ways of posting a reply to
someone else (having basically nothing to do whatsoever with the post you
reply to) but in reality replying to me.
It's better not to respond.

That's why you do it, behind my back, trying to show it up as a reply to
someone else. This all game calling me troll and trying to paint me black
behind my back is childish, just liek your arrogant post I have replied to.
Grow up!
Hence my plea for everyone to "Please don't feed the
...". He'll get over it soon enough.

Please stop being a mind reader. you are very very bad at it. As for you
trying to figure out my intentions - it is not working.

Trolling is what you do. You post flaim baits, and this was nto the first
one. Stop doing it. And get off my back, because I do not appreciate it.
Get a girlfriend and sc**w her. I am not into it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top