This question has already been answered:

there is a class with an overloaded statement <<

template<class T> class Array { private: //... public: //... friend ostream& operator<<(ostream &os, const Array<T> &obj); }; 

The implementation of the overload is such (the implementation, as expected, in the header):

 template<class T> ostream& operator<<(ostream &os, const Array<T> &obj) { //.. return os; } 

In Maine, I write the following code:

 cout << "arr: " << arr << endl; 

As a result, it gives an error:

error: undefined reference to operator << (std :: ostream &, Array const &) '

What is the problem? I have already re-read everything in both Russian and English-speaking form, not one advice helps.

Reported as a duplicate by ixSci members, Community Spirit 30 Oct '16 at 7:53 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Declare a friend (he is yours template) like this:

     template<typename U> friend ostream& operator<<(ostream &os, const Array<U> &obj); 
    • The exact same mistake. Nothing has changed - WenSiL
    • Here, look ... ideone.com/cbPduc - Harry
    • Damn, I do not understand anything ... I did, as you advised and copied all the code directly into main.cpp - it works, but in no way at all. - WenSiL