返回狼盟编程首页
编程搜索 [狼盟旧档]
论坛统计


请输入搜索关键字:
├─◆ 狼盟首页 > 查看贴子 > 详细信息

楼主

问个简单的问题


template <class T>ThrNode<T> *InThrBiTree<T>::Next(ThrNode<T> *p){    ThrNode<T> *q;    if (p->rtag==1) q=p->rchild;     //右标志为1,可直接得到后继结点    else    {        q=p->rchild;        //工作指针初始化,        while(q->ltag==0)   //查找最左下结点            q=q->lchild;    }    return q;}template <class T>void InThrBiTree<T>::InOrder(ThrNode<T> *root){    ThrNode<char> *p;    if (root==NULL) return;  //如果线索链表为空,则空操作返回    p=root;    while(p->ltag==0)          //查找中序遍历序列的第一个结点p并访问    p=p->lchild;    cout<<p->data;    while(p->rchild!=NULL)      //当结点p存在后继,依次访问其后继结点    {        p=Next(p);//报错        cout<<p->data;    }}我是我程序中的两个成员函数p=Next(p);为什么会在这里报错F:xqsC++中序线索二叉树的遍历.cpp(101) : error C2440: '=' : cannot convert from 'struct ThrNode *' to 'struct ThrNode<char> *'应该怎么改下才对呢





tianyuan008 [ 1 楼 ]
2006-05-28 16:54:00
Next成员函数是和InThrBiTree<T>绑在一起的,把ThrNode<char> *p;改成ThrNode<T> *p;就可以了 

sumnet [ 2 楼 ]
2006-05-28 17:18:00
引用:Next成员函数是和InThrBiTree<T>绑在一起的,把ThrNode<char>&nbsp;*p;改成ThrNode<T>&nbsp;*p;就可以了改过还是不行  

tianyuan008 [ 3 楼 ]
2006-05-28 17:39:00
提示呢? 

sumnet [ 4 楼 ]
2006-05-28 17:46:00
还是一样的程序贴上来帮我看看把谢谢拉~~~#include<iostream>using namespace std;enum flag{child,thread};template <class T>struct ThrNode  {    T data;           ThrNode<T> *lchild, *rchild;    flag ltag,rtag;};template <class T>class InThrBiTree{public:    InThrBiTree();                 //~InThrBiTree();    ThrNode *Next(ThrNode<T> *p);    void InOrder(ThrNode<T> *root);    ThrNode<T> *Getroot();private:    ThrNode<T> *root;    ThrNode<T> *pre;    void Creat(ThrNode<T> *&root);         void ThrBiTree(ThrNode<T> *root);   };template <class T>InThrBiTree<T>::InThrBiTree()  {    Creat(root);    pre=NULL;    ThrBiTree(root);}template <class T>void InThrBiTree<T>::Creat(ThrNode<T> *&root){    char ch;    cin>>ch;    if (ch=='#') root=NULL;    //建立一棵空树    else     {         root=new ThrNode<T>;   //生成一个结点,左右标志均置0         root->data=ch; root->ltag=0; root->rtag=0;         Creat(root->lchild);   //递归建立左子树         Creat(root->rchild);   //递归建立右子树    }  }template <class T>void InThrBiTree<T>::ThrBiTree(ThrNode<T> *root){    if (root==NULL) return;    ThrBiTree(root->lchild);    if (!root->lchild)     {               //对root的左指针进行处理        root->ltag=1;           root->lchild=pre;  //设置pre的前驱线索    }    if (!root->rchild) root->rtag=1;   //对root的右指针进行处理    if (pre->rtag==1) pre->rchild=root; //设置pre的后继线索    pre=root;    ThrBiTree(root->rchild);}template<class T>ThrNode<T> *InThrBiTree<T>::Getroot(){    return root;}template <class T>ThrNode<T> *InThrBiTree<T>::Next(ThrNode<T> *p){    ThrNode<T> *q;    if (p->rtag==1) q=p->rchild;     //右标志为1,可直接得到后继结点    else    {        q=p->rchild;        //工作指针初始化,        while(q->ltag==0)   //查找最左下结点            q=q->lchild;    }    return q;}template <class T>void InThrBiTree<T>::InOrder(ThrNode<T> *root){    ThrNode<T> *p;    if (root==NULL) return;  //如果线索链表为空,则空操作返回    p=root;    while(p->ltag==0)          //查找中序遍历序列的第一个结点p并访问    p=p->lchild;    cout<<p->data;    while(p->rchild!=NULL)      //当结点p存在后继,依次访问其后继结点    {        p=Next(p);        cout<<p->data;    }}int main(){    InThrBiTree<char> itbt;    ThrNode<char> *root=itbt.Getroot();    itbt.InOrder(root);} 

tianyuan008 [ 5 楼 ]
2006-05-28 18:02:00
如下程序在 BCB 里编译成功#include <iostream>#include <string.h>#include <stdio.h>using namespace std;template<typename T> class ThrNode{public: int rtag,ltag; ThrNode<T> *rchild,*lchild; T data;};template<typename T> class InThrBiTree{public:   ThrNode<T>* Next(ThrNode<T>*p);   void InOrder(ThrNode<T> *root);};template <class T>ThrNode<T> *InThrBiTree<T>::Next(ThrNode<T> *p){    ThrNode<T> *q;    if (p->rtag==1) q=p->rchild;     //右标志为1,可直接得到后继结点    else    {        q=p->rchild;        //工作指针初始化,        while(q->ltag==0)   //查找最左下结点            q=q->lchild;    }    return q;}template <class T>void InThrBiTree<T>::InOrder(ThrNode<T> *root){    ThrNode<T> *p;    if (root==NULL) return;  //如果线索链表为空,则空操作返回    p=root;    while(p->ltag==0)          //查找中序遍历序列的第一个结点p并访问    p=p->lchild;    cout<<p->data;    while(p->rchild!=NULL)      //当结点p存在后继,依次访问其后继结点    {        p=Next(p);//报错        cout<<p->data;    }}int main(){  InThrBiTree<char> s;  getchar();  return 0;} 

tianyuan008 [ 6 楼 ]
2006-05-28 18:08:00
类模板的声明出问题了,类模板中的声明:ThrNode *Next(ThrNode<T> *p);应该改成:ThrNode<T> *Next(ThrNode<T> *p);