@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.
Printable View
@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.
I believe you asked for advice concerning how to do it.
Apparently it didn't work for some of your chaining.
Regardless, there is usually only one "correct" way of doing post increment, and it is to return a copy.
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.
I'm willing to bet that that is not the entire error message.Quote:
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.Quote:
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::ostream & operator << (std::ostream &, 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.Quote:
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.Quote:
Originally Posted by Subsonics
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.
Discarding const usually means a const method is trying to call a non-const method. Also consider that if you invoke methods on a const object (eg a const reference to T), then all members are considered const.
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.