-3

Overload the relational operator ==for the class stackTypethat returns trueiftwo stacks of the same type are the same; falseif otherwise.

my code:

template <class Type>
const stackType<Type>& stackType<Type>::operator ==
(const stackType<Type>& otherStack)
{
    if (this->stackTop != otherStack.stackTop)
        return false;
    for (int i = 0; i < stackTop; i++)
        if (this->list[i] != otherStack.list[i])
            return false;
    return true;
} //end operator==

I'm receiving a returning reference to local temporary object warning and from what i understand is that it's being destroyed once the function scope ends. Can anyone point me in the right direction to solving this warning?

noslov
  • 21
  • 1

1 Answers1

2

You are declaring your operator to return a const stackType<Type>&, bt then you return true or false. The compiler tries to convert true and false to const stackType<Type>&, and then return it. That’s probably not what you intended - rather declare it to return a bool.

Even if the conversion would work, the reference is to a local variable, which gives UB.

Aganju
  • 6,295
  • 1
  • 12
  • 23
  • Correct, it was meant to be a bool, but i still get a returning to local temp. How do I I not return to local temp? – noslov Oct 22 '17 at 20:50
  • 1
    Did you define it to return a `bool&`? Don't do that, never with elementary data types. Otherwise, the error must come from somewhere else in the code. – Aganju Oct 22 '17 at 21:17