#Why does my return not work in display function?
84 messages · Page 1 of 1 (latest)
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.
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;
}
You can also do return *this
i tried that but get this error
Wait what are you trying to return here
I suspect you really meant to make this a void function and not return anything.
cause you shouldn't return this
this is completly wrong
look at the code Sixshaman shows
returning this will never work here....
no you can't.....
When I try the code sixshaman shows it asks for arguments now in my main
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
If i try this it says its oustream is undefines and type name is not allowed
i am
this is code before it
wait you are trying to overload operator<< or trying to print things to console?
cause those are very diffent things
trying to overload
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);
I have to use the display function though
why?
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
then this is not the way..... for operator overloading....
ima need more context then
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
yea you can credit my godlike prof
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
how would I go about doing that
similar to what you have now realy
but instead of writing to cout write to the local osstream
Yeah not here, my bad
But usually you would do that when overloading operator<< when you output something
not realy as it's for IO so you would return the osstream and not the local object
do you mean like this?
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
How do I create a local ostream variable for it
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
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
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
You cannot just create a local ostream object and return a reference to that.
Your compiler will tell you this.
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
Agreed, this is a BS assignment.
yeah....
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
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.
yeah exactly and thats the entire thing that made me so so confused
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
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.
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
was able to figure it out
!solved