首页 > 代码库 > 2014.10.9 模板函数做友元函数的问题

2014.10.9 模板函数做友元函数的问题

1.模板函数作为类的友元函数,要先声名。

2.在类的定义中声明友元时要在函数后加上<T>

  1 #ifndef Array1D_  2 #define Array1D_  3   4 // One-dimensional arrays.  5   6 #include <iostream>  7 #include "xcept.h"  8   9  10 template <class T> class Array1D; 11 //template <class T> std::istream &operator>>(std::istream &in, Array1D<T> &r);//函数模板做友元要先声明 12 template <class T> std::ostream& operator<<(std::ostream&, const Array1D<T>&); 13  14 template<class T> 15 class Array1D { 16    friend std::ostream& operator<<<T> 17           (std::ostream&, const Array1D<T>&); 18    public: 19       Array1D(int size = 0); 20       Array1D(const Array1D<T>& v); // copy constructor 21       ~Array1D() {delete [] element;} 22       T& operator[](int i) const; 23       int Size() {return size;} 24       Array1D<T>& operator=(const Array1D<T>& v); 25       Array1D<T> operator+() const; // unary + 26       Array1D<T> operator+(const Array1D<T>& v) const; 27       Array1D<T> operator-() const; // unary minus 28       Array1D<T> operator-(const Array1D<T>& v) const; 29       Array1D<T> operator*(const Array1D<T>& v) const; 30       Array1D<T>& operator+=(const T& x); 31       Array1D<T>& ReSize(int sz); 32    private: 33        int size; 34        T *element; // 1D array 35 }; 36  37 template<class T> 38 Array1D<T>::Array1D(int sz) 39 {// Constructor for one-dimensional arrays. 40    if (sz < 0) throw BadInitializers(); 41    size = sz; 42    element = new T[sz]; 43 } 44  45 template<class T> 46 Array1D<T>::Array1D(const Array1D<T>& v) 47 {// Copy constructor for one-dimensional arrays. 48    size = v.size; 49    element = new T[size];  // get space 50    for (int i = 0; i < size; i++) // copy elements 51       element[i] = v.element[i]; 52 } 53  54 template<class T> 55 T& Array1D<T>::operator[](int i) const 56 {// Return reference to element i. 57    if (i < 0 || i >= size) throw OutOfBounds(); 58    return element[i]; 59 } 60  61 template<class T> 62 Array1D<T>& Array1D<T>::operator=(const Array1D<T>& v) 63 {// Overload assignment operator. 64    if (this != &v) {// not self-assignment 65       size = v.size; 66       delete [] element; // free old space 67       element = new T[size]; // get right amount 68       for (int i = 0; i < size; i++) // copy elements 69          element[i] = v.element[i]; 70       } 71    return *this; 72 } 73  74 template<class T> 75 Array1D<T> Array1D<T>:: 76            operator+(const Array1D<T>& v) const 77 {// Return w = (*this) + v. 78    if (size != v.size) throw SizeMismatch(); 79  80    // create result array w 81    Array1D<T> w(size); 82    for (int i = 0; i < size; i++) 83        w.element[i] = element[i] + v.element[i]; 84  85    return w; 86 } 87  88 template<class T> 89 Array1D<T> Array1D<T>:: 90            operator-(const Array1D<T>& v) const 91 {// Return w = (*this) - v. 92    if (size != v.size) throw SizeMismatch(); 93  94    // create result array w 95    Array1D<T> w(size); 96    for (int i = 0; i < size; i++) 97        w.element[i] = element[i] - v.element[i]; 98  99    return w;100 }101 102 template<class T>103 Array1D<T> Array1D<T>::operator-() const104 {// Return w = -(*this).105    // create result array w106    Array1D<T> w(size);107    for (int i = 0; i < size; i++)108        w.element[i] = -element[i];109 110    return w;111 }112 113 template<class T>114 Array1D<T> Array1D<T>::operator*(const Array1D<T>& v) const115 {// Return w = (*this) * v. Pairwise multiply.116    if (size != v.size) throw SizeMismatch();117 118    // create result array w119    Array1D<T> w(size);120    for (int i = 0; i < size; i++)121        w.element[i] = element[i] * v.element[i];122 123    return w;124 }125 126 template<class T>127 Array1D<T>& Array1D<T>::operator+=(const T& x)128 {// Add x to each element of (*this).129    for (int i = 0; i < size; i++)130        element[i] += x;131    return *this;132    }133 134 template<class T>135 std::ostream& operator<<(std::ostream& out,136                     const Array1D<T>& x)137 {// Put the elements of x into the stream out.138    for (int i = 0; i < x.size; i++)139       out << x.element[i] << "  ";140    return out;141 }142 143 template<class T>144 Array1D<T>& Array1D<T>::ReSize(int sz)145 {// Change the size to sz.146  // Do not copy array elements to new space.147    if (sz < 0) throw BadInitializers();148    delete [] element;149    size = sz;150    element = new T [size];151    return *this;152 }153 154 #endif

 以上为本次实验的实例代码,由于没有先进行声明,编译可以通过,执行时会报错

code block:对‘operator<<(std::ostream&, Array1D<int> const&)’未定义的引用VS:error LNK2019: 无法解析的外部符号

2014.10.9 模板函数做友元函数的问题