@User Name:
Can you please read the thread. I'm not trying to do var++++, I originally asked for a "best practice" regarding returning a reference for the sake of chaining operators. That has changed a bit but.
@User Name:
Can you please read the thread. I'm not trying to do var++++, I originally asked for a "best practice" regarding returning a reference for the sake of chaining operators. That has changed a bit but.
>>Can you elaborate on this, I assign the current value to a temp variable, I then update *this and return the temp variable.
Sure, read below:
>>Yes, but it did not return a value on purpose, to demonstrate how that effects the stream.
>>Yes, the increment operation on d has nothing to do with the stream. But if you think about what the difference is between post and prefix increment operations it also has an effect outside of d, in this case the output stream.
Different strokes I guess. The object changes, not cout, until cout performs its operation. I didn't anticipate your conclusion. So it's all still a misunderstanding on our part and we could argue about the one true way. I don't want to.
Anyway, thanks to everyone who have been participating here. I need to find this error elsewhere, what ever it is, it's not related to the cout requiring a reference.
Refer to wikipedia here and you cant go wrong.
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"
I'm willing to bet that that is not the entire error message.no match for operator<< in operator++(int)(0)
If it is, use a different compiler to get a different error message.
GCC is a marvelous compiler, but it doesn't always produce the best error messages.
This is correct, but the error is absolutely related to your not providing an appropriate stream operator for the given context.I need to find this error elsewhere, what ever it is, it's not related to the cout requiring a reference.
I believe that your stream operator looks like `std:stream & operator << (std:stream &, Type &)' which is wrong.
Soma
Thanks iMalc, that is very useful.
@phantomotap
The stream works if I do like this for example:
But not for post incrementCode:A a, b; std::cout << a << (a - b) << (a == b);
It's declared like this:
Code:friend std::ostream & operator<<(std::ostream & os, Type & d);
Yes. That's what I said I believed it to be.It's declared like this
As I said, that is wrong.
Soma
It is wrong because it is not const-correct: the overloaded operator<< should not be modifying the observable state of the object, hence the type of the second parameter should be const Type&, not Type&. The problem that you observed in failing to be const-correct is that when your postfix operator++ returns by value, the temporary returned cannot be bound to this non-const reference parameter.Originally Posted by Subsonics
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Thank you laserlight, for pointing out what is wrong. If I remember correctly I used to have it declared as const but that gave me errors elsewhere. Let me get back once I have tried to compile with the new const in place.
Okay so inside the implementation of operator<< I use three get methods to display private data members. But I got a complaint about discarding const qualifiers with virtual methods. Now I tried to just display the data members directly since operator<< is declared as friend, and it worked. Don't know if that is considered ok, but I suppose so. Thanks again, now the postfix operator is working as it should without returning a reference.
Thanks Elysia. I'm inspecting that right now, after compiling a test file that generated quite a few of these errors. So, adding const to these methods should solve the problem then?
If you forgot to add const to method that do not change the class's member variables, then yes, it will solve it. But if that isn't true, then you may have to evaluate your const chain.
If a method calls a non-const method, then it make take a non-const reference of any arguments it intend to modify, in the present state or the future.