Your vector is a template class. Hence the compiler knows no Vector. He only knows Vector<...someType...>.
So you can write something like this
std::ostream& operator << (std::ostream & o, const Vector<int> & v){
return o;
}
With this function, a minimal working example which doesn't modify the header would be:
template <typename T>
class Vector
{
private:
int sz;
T* buf;
public:
// member functions all the way down
friend std::ostream& operator<<(std::ostream& o, const Vector& v);
};
std::ostream& operator<<(std::ostream& o, const Vector<int>& v)
{
return o;
}
int main()
{
Vector<int> a;
std::cout << a << std::endl;
}
However, to get a general offstream operator, you have to use another template:
template <typename T>
std::ostream& operator<<(std::ostream& o, const Vector<T>& v)
{
return o;
}
In this case, the implementation should stay in the header. Otherwise, you can use it only in your Vector.cpp because templates do not produce any code until they are instantiated. As a result, every file that wants to use the operator needs to know the implementation of the template to generate the necessary code.
Also, in order to work correctly, you have to adjust the friend declaration:
template <typename T2>
friend std::ostream& operator<<(std::ostream& o, const Vector<T2>& v);
For a smarter way to adjust the friend declaration have a look at the answer of R Sahu