#include<iostream> using namespace std; template<typename T> struct Node { T data; Node<T> *next; }; template<class T> class Set { friend ostream& operator<< <>(ostream& out, const Set<T>& s); public: Set() { head = new Node<T>; head->next = NULL; } Set(const T& s) { Node<T> *p, *pnew, *ptail; p = s.head->next; ptail = head; while (p) { pnew = new Node<T>; ptail->next = pnew; pnew->date = p->data; p = p->next; ptail = pnew; } ptail->next = NULL; } ~Set() { Node<T>* p; while (head!=NULL) { p = head->next; delete head; head = p; } } void print(); void in(); private: Node<T> *head; }; template<typename T> void Set<T>::in() { int i, n; Node<T> *p, *pnew; p = head; cout << "input number" << endl; cin >> n; for (i = 0; i < n; i++) { pnew = new Node<T>; cin >> pnew->data; p->next = pnew; p = pnew; } p->next = NULL; } template<typename T> ostream& operator<<(ostream& out, const Set<T>& s) { Node<T> *p; p = s.head->next; while (p) { cout << p->data << " "; p = p->next; } return out; } void main() { Set<int> s; s.in(); cout << s<<endl; Set<int> a(s); cout << a << endl; }
编译出错,把析构函数注释就成功了,百思不得旗解,感觉析构写对的啊,
解决方案
20
复制构造函数参数
Set(const T& s) -> Set(const Set<T>& s)
当然,这函数还是很有问题,例如date与data,还有头指针的初始化,不过这让lz本人解决的好
Set(const T& s) -> Set(const Set<T>& s)
当然,这函数还是很有问题,例如date与data,还有头指针的初始化,不过这让lz本人解决的好