When building an error occurs:
#include <iostream> const int DefaultSize=10; using namespace std; template <class T> class Array { public: Array(int itsSize=DefaultSize); Array(const Array& rhs); ~Array() {delete [] pType;} Array& operator = (const Array&); T& operator[] (int offset){return pType[offset];} const T& operator [] (int offset) const {return pType[offset];} friend void Intrude(Array<int>); friend ostream& operator<< (ostream&, Array<T>&); // < Компилятор указывает на int GetSize() const {return itsSize;} //эту строку private: T *pType; int itsSize; }; template <class T> ostream& operator << (ostream& output, Array<T>& theArray) { for (int i=0;i<theArray.GetSize();i++) output<< "["<<i<<"]"<<theArray[i]<<endl; return output; }
compile report:
g ++ -Wall -c "template.cpp" (in the directory: / host / projects / template) template.cpp: 30: 52: warning: friend declaration 'std :: ostream &> operator << (std :: ostream &, Array < t> &) 'declares a non-template function template.cpp: 30: 52: note: (you need to make it a function> The build was successful.
assembly report:
g ++ -Wall -o "template" "template.cpp" (in the directory: / host / projects / template) template.cpp: 30: 52: warning: friend declaration 'std :: ostream &> operator << (std :: ostream & , Array <t> &) 'declares a non-template function template.cpp: 30: 52: note: (if you’re not what you intended, make a function name here) /tmp/cciPebmn.o: In function
main': template.cpp:(.text+0x162): undefined reference to >
operator << (std :: basic_ostream <char, std :: char_traits <char = "" >> &, Array <int> &) 'collect2: ld returned 1 exit status The assembly failed with an error.
Well, actually the question: what is the mistake?