#Why does my return not work in display function?

84 messages · Page 1 of 1 (latest)

ruby berry
#

I'm trying to return my display but not sure what this error message means in my context. "ERROR: Initial value of reference to non-const must be lvalue" !solved

errant sequoiaBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

proven salmon
#

Your function should return std::ostream&, but this has type Ticket*

#

You probably wanted to make ostream the parameter of the function and use it instead of cout

#
std::ostream& Ticket::display(std::ostream& outStream) const
{
    outStream << "<Put whatever you want to print here>";    

    return outStream;
}
minor hamlet
#

You can also do return *this

ruby berry
minor hamlet
#

Wait what are you trying to return here

modest yarrow
#

I suspect you really meant to make this a void function and not return anything.

unreal beacon
#

this is completly wrong

#

look at the code Sixshaman shows

#

returning this will never work here....

unreal beacon
ruby berry
unreal beacon
#

well yeah of it doesn't work?

#

ugh wait

#

2 awnsers are mixing things

#

from the original post I thought you where traing to overload the operator<< for some reason

ruby berry
#

If i try this it says its oustream is undefines and type name is not allowed

#

i am

#

this is code before it

unreal beacon
#

wait you are trying to overload operator<< or trying to print things to console?

#

cause those are very diffent things

ruby berry
#

trying to overload

unreal beacon
#

okay then yeah your current syntax is completly wrong

#
std::ostream& operator<<(std::ostream& os, const T& obj)
{
    // write obj to stream
    return os;
}
#

is the overload operator

#
// overload_date.cpp
// compile with: /EHsc
#include <iostream>
using namespace std;

class Date
{
    int mo, da, yr;
public:
    Date(int m, int d, int y)
    {
        mo = m; da = d; yr = y;
    }
    friend ostream& operator<<(ostream& os, const Date& dt);
};

ostream& operator<<(ostream& os, const Date& dt)
{
    os << dt.mo << '/' << dt.da << '/' << dt.yr;
    return os;
}

int main()
{
    Date dt(5, 6, 92);
    cout << dt;
}

from the ms docs

#

if you want to avoid the friend function and make it a member I believe the proper definition is:
std::ostream& operator<<(std::ostream& os);

ruby berry
#

I have to use the display function though

unreal beacon
#

why?

ruby berry
#

It's required by my professor

#

the overload function above the display was given by my professor I just need to figure out the display

unreal beacon
#

then this is not the way..... for operator overloading....

#

ima need more context then

ruby berry
#

I can give all my code or the instructions of what im trying to accomplish

#

So i have to return the details of the ticket after checking with if statement

unreal beacon
#

ooooooooooh

#

man, that is dumb

ruby berry
#

yea you can credit my godlike prof

unreal beacon
#

then just construct a local std::osstream object and return that and write to the ossstream

#

the 2nd point is iffy at best as well

#

anyhow

ruby berry
#

how would I go about doing that

unreal beacon
#

similar to what you have now realy

#

but instead of writing to cout write to the local osstream

minor hamlet
unreal beacon
unreal beacon
#

yeah and that returns a ostream....

#

returning *this would return the local object

ruby berry
unreal beacon
#

no, that is doing the exact oposite of what I said.....

#

create a local ostream variable

#

then write to that, and at the end return that

ruby berry
#

How do I create a local ostream variable for it

unreal beacon
#

hu this is fucking hillarious....

#

after further looking into things im pretty sure you actually can't make local osstream

#

what makes this 100% hillarious cause that means the exercise is idiotic

ruby berry
#

yea we dont really get a lot of lecture on understanding

#

just shows us code an goes over a few things

#

were basically expected to know stuff that half of which wasnt taught to us in the previous course

#

paying to self teach so it be like that

unreal beacon
#

okay I came up with something:

#include <iostream>
#include <string>

std::ostream& foo()
{
    return std::cout << "a";
}

int main() {
    foo() << " hello world";
}
#

there we go

modest yarrow
#

You cannot just create a local ostream object and return a reference to that.
Your compiler will tell you this.

unreal beacon
#

im dead inside now and this is idiotic

#

well I guess it's tehcnically possible with this.... to comply to the exercise but this is 1000000 levels of cursed

modest yarrow
#

Agreed, this is a BS assignment.

unreal beacon
#

ill admit the local ostream was kinda dumb 😅, but I figured it had to be that as that would make some inkling of sense

#

but never actually tried making one locally, cause well thats dumb

modest yarrow
#

You only involve an ostream if you overload the free function streaming operator, so you could write code like:

for (unsigned int count = 0; count < 10; count++)
  cout << airCanada[count];

This code does not require there to be a Ticket::display() function.

The operator may of course use Ticket::display() instead of being made a friend to the class.
But doing so would required the ostream to be passed - by non-const reference - into said function as a parameter.

unreal beacon
#

like the only way I can think of to make this work is the above I posted.... and that is just beyond levels of cursed

modest yarrow
#

BS assignment.

#

The only way to abide by the restrictions placed upon the code as-is, would be to:

std::ostream& Ticket::display() const
{
  // code as before, then...
  return cout;
}

But that's insane.
Why would a function return a reference to a global variable?

This would not survive a peer review in any self respecting company. It smells.

unreal beacon
#

yeah...

#

it's just weird

#

a bit of a rant but I loath a lot of uni assigments cause they are non sensical and weirdly arbetrarely restricted

ruby berry
#

was able to figure it out

ruby berry
#

!solved