下面这种定义方式会报错: Undefined symbols for architecture x86_64
template <typename T> class longint{ public: T num; longint (T a = 0) { num = a; } friend ostream& operator<< (ostream& out, const longint<T>& Lint); ~longint (){ ; } private: }; template <typename T> ostream& operator<< (ostream &out, const longint<T> & Lint) { return out<<Lint.num; }
改法一:将重载运算符的T改成S就行了
//类内声明时 template <typename S> friend ostream& operator<< (ostream& out, const longint<S>& Lint); //类外定义时 template <typename S> ostream& operator<< (ostream &out, const longint<S> & Lint) { return out<<Lint.num; }
改法二:直接在类内定义
friend ostream& operator<< (ostream& out, const longint<T>& Lint) { return out<<Lint.num; }
时间: 2024-10-11 23:55:21