dragondaddy63 2022-05-18 19:41 采纳率: 80%
浏览 17
已结题

为什么qsize没有被linkquene继承?


#include<iostream>
#include<assert.h>
using namespace std;

template <class type>
class abque{
    public:
        bool IsEmpty()
        {
            return (qsize==0)? true:false;
        }
        virtual void PushTail(type &)=0;
        virtual bool PopHead(type &)=0;
        virtual void Clear()=0;
    protected:
        int qsize;
};

template <class type>
class quenenode{
    public:
        type data;
        quenenode* next;
};

template <class type>
class linkquene:public abque<type>{
    public:
        linkquene();
        linkquene(linkquene &q)
        {
            head=NULL;
            tail=NULL;
            Copy(q);
        }
        ~linkquene()
        {
            Clear();
        }
        void PushTail(type &);
        bool PopHead(type &);
        void Clear();
        linkquene& operator=(linkquene &q)
        {
            Copy(q);
            return *this;
        }
    protected:
        quenenode<type> *head;
        quenenode<type> *tail;
        linkquene &Copy(linkquene &q);
};

template<class type>
linkquene<type>::linkquene()
{
    qsize=0;
    head=tail=NULL;    
};

template<class type>
void linkquene<type>::PushTail(type & x)
{
    quenenode<type>* p;
    p=new quenenode<type>;
    assert(p);
    if(p==NULL)
    {
        cout<<"out of memory"<<endl;
        exit(1);
    }
    p->data=x;
    if (tail)
    {
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    else
    {
        p->next=NULL;
        tail=p;
        head=p;
    }
    qsize++;
};

template<class type>
bool linkquene<type>::PopHead(type &x) 
{
    quenenode<type> *p;
    if (head)
    {
        x=head->data;
        p=head;
        head=head->next;
        if(head==NULL)
        {
            tail=NULL;
        }
        delete p;
        qsize--;
        return true;
    }
    return false;
}

template<class type>
linkquene<type> &linkquene<type>::Copy(linkquene<type> &que)
{
    quenenode<type> *p,*q,*r;
    if (head)
        Clear();
    qsize=que.qsize;
    head=tail=NULL;
    if (!que.head)
        return *this;
    head= new quenenode<type>;
    assert(head);
    if (head==NULL)
    {
        cout<<"out of memory"<<endl;
        exit(1);
    }
    head->data=(que.head)->data;
    head->next=NULL;
    tail=head;
    r=NULL;
    p=head;
    q=que.head->next;
    while(q)
    {
        r=new quenenode<type>;
        assert(r);
        if (r==NULL)
        {
            cout<<"out of memory"<<endl;
            exit(1);
        }
        r->data=q->data;
        r->next=NULL;
        p->next=r;
        tail=r;
        p=p->next;
        q=q->next;
    }
    return *this;
}

template<class type>
void linkquene<type>::Clear()
{
    type p;
    while(PopHead(p));
    head=tail=NULL;
}

int main()
{
    int n,i,x;
    linkquene<int> quene1,quene2,quene3;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>x;
        if (x<60) quene1.PushTail(x);
        if (x>=60&&x<100) quene2.PushTail(x);
        if (x>=100) quene3.PushTail(x);
    }
}

为什么qsize没有被linkquene继承?如果在linkquene中重新定义一个qsize,就没有问题了。

  • 写回答

2条回答 默认 最新

  • 张十五 2022-05-19 15:20
    关注

    两阶段名称查找。第一阶段模板类未被创建,所以找不到基类的成员,报错,第二阶段,类型确定,模板类建出来了,找到了,就好了。

    //延迟到第二阶段查找
     abque<type>::qsize;
    //或者
    this->qsize;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月28日
  • 已采纳回答 5月20日
  • 创建了问题 5月18日

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分