QQ懒羊羊QQ QQ懒羊羊QQ
足球迷
关注数: 15 粉丝数: 215 发帖数: 5,061 关注贴吧数: 27
模版类里面设 另一个模版类的成员函数为友元问题 #ifndef BINARYTREE_H #define BINARYTREE_H #include"llqueue.h" #include"tnode.h" #include"stack.h" #include"xcept.h" #include"minheap.h" int _count; template<class T> class Huffman { public: friend BinaryTree<int> HuffmanTree(T [], int ); operator T () const {return weight;} private: BinaryTree<int> tree; T weight; }; template<class T> class BinaryTree { public: BinaryTree() {root = 0;}; ~BinaryTree(){}; bool IsEmpty() const {return ((root) ? 0 : 1);} bool Root(T& x) const; void MakeTree(const T& element,BinaryTree<T>& left, BinaryTree<T>& right); void BreakTree(T& element, BinaryTree<T>& left, BinaryTree<T>& right); void PreOrder(void(*Visit)(BinaryTreeNode<T> *u)) {PreOrder(Visit, root);} void InOrder(void(*Visit)(BinaryTreeNode<T> *u)) {InOrder(Visit, root);} void PostOrder(void(*Visit)(BinaryTreeNode<T> *u)) {PostOrder(Visit, root);} void LevelOrder(void(*Visit)(BinaryTreeNode<T> *u)); void PreOutput() {PreOrder(Output, root); cout << endl;} void InOutput() {InOrder(Output, root); cout << endl;} void PostOutput() {PostOrder(Output, root); cout << endl;} void LevelOutput() {LevelOrder(Output); cout << endl;} void Delete() {PostOrder(Free, root); root = 0;} int Height() const {return Height(root);} int Size() {_count = 0; PreOrder(Add, root); return _count;} BinaryTree<int> HuffmanTree(T [], int ); private: BinaryTreeNode<T> *root; void PreOrder(void(*Visit)(BinaryTreeNode<T> *u),BinaryTreeNode<T> *t); void InOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t); void PostOrder(void(*Visit)(BinaryTreeNode<T> *u), BinaryTreeNode<T> *t); static void Output(BinaryTreeNode<T> *t) { CString s; s.Format("%c",t->data); STR+=s; } static void Free(BinaryTreeNode<T> *t) {delete t;} static void Add(BinaryTreeNode<T> *t) {_count++;} int Height(BinaryTreeNode<T> *t) const; }; 中间省略 template <class T> BinaryTree<int> BinaryTree<T>::HuffmanTree(T a[], int n) { Huffman<T> *w=new Huffman<T>[n+1]; BinaryTree<int> z,zero; int num=0; for(int i=1;i<=n;i++) { if(a[i]==0) continue; z.MakeTree(i,zero,zero); w[i].weight=a[i]; w[i].tree=z; num++; } MinHeap<Huffman<T>> H(1); H.Initialize(w,n,n); Huffman<T> x,y; for(int i=1;i<num;i++) { H.DeleteMin(x); H.DeleteMin(y); z.MakeTree(0,x.tree,y.tree); x.weight+=y.weight; x.tree=z; H.Insert(x); } H.DeleteMin(x); H.Deactivate(); delete[]w; return x.tree; } #endif e:\工程文件\huffman\huffman\binarytree.h(199): error C2248: “Huffman<T>::weight”: 无法访问 private 成员(在“Huffman<T>”类中声明) 1>e:\工程文件\huffman\huffman\binarytree.h(200): error C2248: “Huffman<T>::tree”: 无法访问 private 成员(在“Huffman<T>”类中声明)
首页 1 2 3 下一页