-1
template <typename T>
bool operator==(const Stack<T>& a, const Stack<T>& b)

i must implement the stack adt using an STL (i have chosen vector)

I am having trouble overloading this nonmember global function. I have a vector in the private section and I wanted to compare each element by popping and checking the top to see if they are equal, however, I can't do this because the stacks are const. Can someone please guide me towards the right direction?

  • Don't `pop`. `peek`. – user4581301 Mar 03 '16 at 23:38
  • Does it have to be a global function? If not, just implement the member function and manually inspect the elements without popping anything? – James Adkison Mar 03 '16 at 23:38
  • 2
    Can also declare the function to be a `friend` of `Stack` and then compare the `vectors` directly. – user4581301 Mar 03 '16 at 23:39
  • it has to be a global function and peek isn't one of the member functions that i needed to define – ajuliettesoc Mar 03 '16 at 23:41
  • Implementing `==` in a way which destroys the objects which are being compared is a bit, shall we say, *unfriendly*. It's not by accident that `==` normally takes const operands. – rici Mar 03 '16 at 23:48
  • If your `Stack` contains a `vector`, why not simply use the [std::vector::operator==](http://en.cppreference.com/w/cpp/container/vector/operator_cmp) to compare the two wrapped vectors? Another way to avoid doing the element-by-element comparison is to use [std::equal](http://en.cppreference.com/w/cpp/algorithm/equal). But if you really want to implement the element-by-element comparison yourself, both `std::vector::size` and `std::vector::operator[]` are available for `const`. –  Mar 04 '16 at 00:07

1 Answers1

1

Why don't you just use operator== on the std::vector data objects? All you need is a friend declaration:

template<typename T> class Stack {
  /* The following allows operator== to use private members */
  friend bool operator==(const Stack&, const Stack&);
  public: /* ... */
  private:
    std::vector<T> data_;
};

template<typename T>
bool operator==(const Stack& a, const Stack& b) {
  return a.data_ == b.data_;
}
rici
  • 234,347
  • 28
  • 237
  • 341
  • Or you can define `operator==` as a member function. There's some oddities when you do that (which I can never remember the details of) but you could also define some other method name as a member function, then call that from a non-member `operator==`. There's nothing wrong with using `friend`, of course, just listing alternatives. –  Mar 04 '16 at 00:11
  • 1
    @Steve314: The OP included the desired prototype which is clearly a non-member function. And the non-member function is a better solution, which is presumably why the professor suggested that prototype. (Discussion at http://stackoverflow.com/questions/4622330/operator-overloading-member-function-vs-non-member-function ; there are probably others.) – rici Mar 04 '16 at 00:13
  • OP doesn't explicitly state that the professor provided that prototype, though maybe that's implied. Even so, I only claimed that there are alternatives. Learning C++ doesn't purely mean keeping your teacher happy. –  Mar 04 '16 at 00:21