Your code has two error:

1) You cannot define friend function body in the class declaration

2) You must declare friend function as template

3) In push function you must increment _index

The following code compile and run right

Qt Code:
  1. #include <iosfwd>
  2.  
  3. template <class T = int, size_t N = 100>
  4. class Stack {
  5. T _data[N];
  6. size_t _index;
  7.  
  8. public:
  9. Stack() : _index(0) { }
  10. void push(const T& value) {
  11. if ( _index != N)
  12. {
  13. _data[_index] = value;
  14. ++_index;
  15. }
  16. else
  17. printf("stack full\n");
  18. }
  19.  
  20. template <typename TYPE, size_t SIZE>
  21. friend std::ostream& operator<<(std::ostream& os, const Stack<TYPE, SIZE>& stack);
  22. };
  23.  
  24. template <typename T, size_t N>
  25. std::ostream& operator<<(std::ostream& os, const Stack<T, N>& stack)
  26. {
  27. for (size_t i=0; i < stack._index; ++i)
  28. os << stack._data[i] << " ";
  29. return os;
  30. }
To copy to clipboard, switch view to plain text mode 

Qt Code:
  1. int main()
  2. {
  3. Stack<int, 10> st;
  4. st.push(2);
  5. st.push(5);
  6. st.push(3);
  7.  
  8. std::cout << st << std::endl;
  9.  
  10. return 0;
  11. }
To copy to clipboard, switch view to plain text mode