Wait, what?
Wait, what?
>> Well, does "very rarely" mean that it won't work, or does it mean that I've completely skated around the issue?
It means "you can do it but then why unless you have to?". There are times, like when invoking an operator from a base class, that you *will* need to to it, but again, that's typically rare.
>> And what do you mean "defined << for [my] class?" Isn't "<<" already defined somewhere in the vast wonderful libraries that I take for granted?
For some *other* class, yes, but for yours - how could it?
Last edited by Sebastiani; 11-30-2008 at 06:19 PM. Reason: repetition of repetition
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
You keep repeating this:
and it worries me. Have you never really seen a+b in a program before? For integers, or for floating-point (float or double), or anything?If I had two integers, a and b, how would I add them like that?
>> and it worries me. Have you never really seen a+b in a program before? For integers, or for floating-point (float or double), or anything?
Me, too. I decided against posting a complete example for similar reasons.
Could you post some code, OP, so that we can see where you are at on this?
Last edited by Sebastiani; 11-30-2008 at 06:37 PM. Reason: fixed typose, restatement
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
it's ok. i got a friend at another school to help me. I suppose the reason I sound so inept is because i'm a little stressed out by not being able to figure this out on my own. I'm only in my second semester of C++, and yes, I do know how to add normal integers. I just don't understand the point of adding them like this and perhaps that's why I can't reason my way through it. I also know a bit of Java (dabbled over the summer) and learned Pascal in my introduction classes quite a few years ago.
So, thanks for all your help!
I'm writing it like this and hopefully it will work:
Code:class rationalADT { int operator+(int a, int b) { int T; T = a + b; return T; }
The compiler already knows how to add an int and an int together. It won't allow you to tell it otherwise. What you need to tell the compiler how to do is to add two Rationals together. If you have a rational a, and a rational b, how do you define a+b? That's the question that you need to answer. There is no point in trying to define addition for integers, that's already built in. You need to add the new stuff in your class.
Shouldn't rationalADT be the arguments to the overloaded operator? Didn't bother to read the posts before this, did you?
Code:#include <cmath> #include <complex> bool euler_flip(bool value) { return std::pow ( std::complex<float>(std::exp(1.0)), std::complex<float>(0, 1) * std::complex<float>(std::atan(1.0) *(1 << (value + 2))) ).real() < 0; }
no... the instructions state explicitly NOT to add the rationals. I just wanted to know how to write the addition like this so that i could figure everything else out. I think i have it now.
Thanks (snide comments aside) though!
Last edited by tabstop; 11-30-2008 at 08:47 PM.
Not only does that not work because it is already defined for ints, but even if one were to replace int with SomeClass then it still wouldn't work because it would be infinitely recursive, which you may have noticed if you simplified it to:Code:int operator+(int a, int b) { int T; T = a + b; return T; }
This is just one of the reasons that when you're introduced to operator overloading you will be given an example like the Fraction one you were given earlier. It only makes sense to define an operator for some kind of useful class, and not somthing that is not built-in.Code:int operator+(int a, int b) { return a + b; }
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
That said, note that some things in your class might be implicitly defined if you do not define them, namely, the default constructor (which will not be implicitly defined if any constructor is user defined), copy constructor, copy assignment operator (another case of operator overloading!) and destructor.Originally Posted by tabstop
Speaking of overloading operator+ for a class: semantically, operator+ would not change the state of the class, so it should be a const member function. However, if you implement a corresponding operator+= instead, then you could make operator+ a non-member non-friend function by implementing it in terms of operator+=.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)