mercredi 21 février 2018

c++11 ambiguous << overload

I have a namespace containing a class that is supposed to mimic the functionalty of a stack, inside I have a print funciton and a out stream overload that calls the print function to print out the contents of a template of the vector in my stack.h file I define those funtions as

namespace foo
  { template <typename T>
    class Stack{
        public:
          //...
          void print(std::ostream& os,char ofs = ' ');
         //...
        private:
               std::vector<T> stack;
             };
     template <typename T>
     std::ostream&  operator<<(std::ostream& os, const Stack<T>& A);
    }

and in my stack.hpp file

template<typename T>
 void foo::Stack<T>::print(std::ostream& os, char ofc)
 {
        for(auto itr = stack.begin(); itr != stack.end(); ++itr)
        {
               os << *itr << ofc;
        }
 }
 template<typename T>
 std::ostream& foo::operator<<(std::ostream& os, const Stack<T>& A)
 {
            A.print(os);
            return os;
 }

then in my stack.cpp driver I make a stack object "b" and fill it with integers and call the .stack::print function and it prints out the elements with the ofc fill charater inbetween but when I call cout << b; the compiler complains that the operator<< overload is abiguous saying," operand types are 'std::ostream { aka std::basic_ostream}' and ' this::Stack')" I am confused because my print function works flawlessly and my overload just calls that function shouldn't work just the same?

Aucun commentaire:

Enregistrer un commentaire