vs2013编译无法通过——数据结构算法“树”,调试显示“尝试引用已删除的函数”,求解
程序代码:
/*Tree.h*/ #include<list> #include<iostream> using namespace std; template<class T> class Tree{ T*NA; list<int>*HL; int root; int sizeN,sizeC; int maxN; public: Tree(int n = 100):root(-1), sizeN(0), sizeC(0), maxN(n){ NA = new T[n]; HL = new list<int>[n]; } ~Tree(){ delete[]NA; delete[]HL; } int Empty()const{ return size == 0; } int Full()const{ return size == maxN; } int SizeN()const{ return sizeN; } int SizeC()const{ return sizeC; } int FindNode(const T&node)const; bool FindNode(const T&node, int pos)const; bool InsertN(const T&node); bool InsertC(const T&parent, const T&child); void readTree(const char*filename); void writeTree(const char*filename); friend ostream&operator<<(ostream ostr, const Tree<T>&t); friend istream&operator>>(istream istr, Tree<T>&t); }; template<class T> int Tree<T>::FindNode(const T&node)const{ for (int i = 0; i < size; i++) if (NA[i] == node) return(i); return -1; } template<class T> bool Tree<T>::FindNode(const T&node, int pos)const{ if (pos < 0 || pos >= sizeN) return 0; node = NA[pos]; return 1; } template<class T> bool Tree<T>::InsertN(const T&node){ if (sizeN == maxN) return 0; NA[sizeN] == node; sizeN++; return 1; } template<class T> bool Tree<T>::InsertC(const T&parent, const T&child){ int pi = FindNode(parent), cj = FindNode(child); if (pi == -1 || cj == -1 || pi == cj) return 0; HL[pi].insert(HL[pi].end(), cj); sizeC++; return 1; } template<class T> istream &operator>>(istream &istr, Tree<T>&t){ char str[50]; int n; T parent, child; istr >> str >> n; istr >> str >> parent; t.InsertN(parent); t.root = t.sizeN - 1; istr << str; for (int i = 1; i <= n - 1; ++i){ istr >> child; t.InsertN(child); } istr >> str >> n; for (int i = 1; i <= n; ++i){ istr >> parent >> child; t.InsertC(parent, child); } return istr; } template<class T> ostream &operator<<(ostream&ostr, const Tree<T>&t){ for (int i = 0; i < t.sizeN; i++){ ostr << i << '-' << t.NA[i] << ':'; List<int>::iterator first = t.HL[i].begin(), last = t.HL[i].end(); for (; fiist != last; ++first) ostr << '(' << *first << ')' << ' '; ostr << endl; } return (ostr); }
程序代码:
/*main*/ #include<iostream> #include<fstream> #include<string> #include"Tree.h" using namespace std; int main(){ Tree<char>T(20); ifstream fin; fin.open("E:\\计算机语言\\c++\\c++程序\\课本例题\\Tree类\\Tree类\\treein.txt", ios::in || ios::_Nocreate); if (!fin){ cout << "can not open E:\\treein.txt.\n"; exit(1); } fin>>T; ofstream fout; fout.open("E:\\计算机语言\\c++\\c++程序\\课本例题\\Tree类\\Tree类\\treeout.txt", ios::out); if (!fout){ cout << "cannot open E:\\treeout.txt.\n"; exit(1); } fout << T; return 0; }