How to overload operator<< to output a vector, defined inside a template?

Viewed 373

The example below is a distilled version of a problem with templates I'm having - please see compilation errors below.

#include <iostream>
#include <vector>

template <class T> struct S
{
  typedef std::vector<T> SVec;
};

template <class T> std::ostream& operator<<(std::ostream& OS, const S<T>::SVec& X)
{
  for (const auto& e: X) OS << e << ' ';
  return OS;
}

int main()
{
  S<int>::SVec v;
  std::cout << v << std::endl;
}

Compiler output:

g++ -g -Wall -O4 -std=c++11 -c tc041.cpp
tc041.cpp:22:69: error: need ‘typename’ before ‘S<T>::SVec’ because ‘S<T>’ is a dependent scope
 template <class T> std::ostream& operator<<(std::ostream& OS, const S<T>::SVec& X)

And so on - hundreds of lines. My compiler - g++ 5.2.1, OS - Xubuntu 4.2.0.

How to make this operator to be compiled correctly?

1 Answers
Related