白杨木の影子 2022-01-21 11:37 采纳率: 100%
浏览 618
已结题

刚开始学数据结构与算法,编译出现错误:[Error] candidate is: virtual int& chain::get(int) const,什么问题呢?

#include<iostream>
#include<fstream>
using namespace std;

//链表节点的结构定义

struct chainNode
{
    //数据成员
    int element;//数据域
    chainNode* next;//指针域
    
    //方法:构造函数形成重载 
    chainNode(){}
    chainNode(const int& element)
    {
        this->element=element;
    }
    chainNode(const int& element,chainNode* next)
    {
        this->element=element;
        this->next=next;
    }
};


class linearList
{
    public:
        virtual ~linearList(){};
        virtual bool empty()const=0;
        virtual int size()const=0;
        virtual int& get(int theIndex)const=0;
        virtual int indexOf(const int& theElement)const=0;
        virtual void erase(int theIndex)=0;
        virtual void insert(int theIndex,const int& theElement)=0;
        virtual void output(ostream& out)const=0;
};


class chain:public linearList
{
    public:
        //构造函数,复制构造函数和析构函数 
    chain(int initCapacity=10);
    chain(const chain&);
    ~chain();
    
    //方法
    bool empty()const
    {
        return listSize==0;
    }
    int size()const
    {
        return listSize;
    }
    int& get(int theIndex)const;
    int indexOf(const int& theElement)const;
    void erase(int theIndex);
    void insert(int theIndex,const int& theElement);
    void output(ostream& out)const;
    
    private:
        chainNode* firstNode;//指向链表第一个节点的指针,也是链表的唯一对外接口 
        int listSize;//线性表的元素个数 
};

//链表的构造和复制构造函数

chain::chain(int initCapacity)
{
    //构造函数
    //判断是否合法 
    if(initCapacity<1)
    {
        cout<<"Initial Capacity = "<<initCapacity<<" must be > 0"<<endl;
        return;
    }
    firstNode=NULL;
    listSize=0;
}


chain::chain(const chain& theList)
{
    //复制构造函数
    listSize=theList.listSize;
    if(listSize==0)
    {
        //如果原链表为空
        firstNode=NULL;
        return;
    }
    //链表theList非空
    chainNode* sourceNode=theList.firstNode;
    //先复制首元素 
    firstNode=new chainNode(sourceNode->element);//为新链申请一块新的内存空间
    sourceNode=sourceNode->next;
    chainNode* targetNode=firstNode;//创建targetNode的原因是:链表必须有firstNode,不可以直接使用它
    //到了此处,targetNode指向新链的第一个节点;sourceNode指向原链的第二个节点,错开一个节点才可以实现复制 
    while(sourceNode!=NULL)
    {
        //复制剩余元素
        targetNode->next=new chainNode(sourceNode->element);
        //此节点复制完成,指针向后移动1
        targetNode=targetNode->next;
        sourceNode=sourceNode->next; 
    }
    //节点全部复制完成,尾节点指针域还要指向空
    targetNode->next=NULL; 
}


chain::~chain()
{
    //链表的析构函数,删除链表的所有节点(销毁链表)
    //算法思想:重复清除链表的首节点,直到链表为空,在清除首节点之前必须用变量nextNode保存第二个节点的指针,否则无法找到下一个节点的地址
    while(firstNode!=NULL)
    {
        //删除首节点
        chainNode* nextNode=firstNode->next;
        delete firstNode;
        firstNode=nextNode;
    }
}

//方法get:返回索引为theIndex的元素(数据域) 

int& chain::get(const int theIndex)const
{
    //若该元素不存在
    if(theIndex<0||theIndex>listSize-1)
    {
        cout<<"out of range!"<<endl;
        return -1;
    }
    chainNode* currentNode=firstNode;//需要新开一个指针,不可以直接改变firstNode
    for(int i=0;i<theIndex;i++)
    {
        currentNode=currentNode->next;
    }
    return currentNode->element;
}

//方法indexOf:返回元素theElement首次出现时的索引

int chain::indexOf(const int& theElement)const
{
    chainNode* currentNode=firstNode;
    int index=0;
    while(currentNode!=NULL&&currentNode->element!=theElement)//结束条件1:遍历完没有找到;结束条件2:找到了 
    {
        index++;
        currentNode=currentNode->next;
    }
    //确定是否找到 
    if(currentNode==NULL)
    {
        return -1;
    }
    else
    {
        return index;
    }
}

//方法erase:删除索引为theIndex的元素 
//三种情况:1、theIndex<0或者theIndex>=listSize,没有这个位置上的元素,或者链表为空
//2、删除非空表的第0个元素节点
//3、删除其他元素节点

void chain::erase(int theIndex)
{
    //索引无效
    if(theIndex<0||theIndex>=listSize)
    {
        cout<<"out of range!"<<endl;
        return;
    }
    //索引有效
    chainNode* deleteNode;
    if(theIndex==0)
    {
        //删除链表首节点
        deleteNode=firstNode;
        firstNode=firstNode->next; 
    }
    //删除其他节点
    else
    {
        //用指针p指向要删除节点的 前驱节点(如果是双向链表,则可以直接指到被删除节点) 
        chainNode* p;
        for(int i=0;i<theIndex-1;i++)
        {
            p=p->next;
        }
        deleteNode=p->next;
        p->next=p->next->next;//删除deleteNode指向的节点 
    }
    listSize--;//链表节点个数减一 
    delete deleteNode;
}

//方法insert:在索引为theIndex的位置插入一个新元素(过程和erase类似)

void chain::insert(int theIndex,const int& theElement)
{
    //索引无效
    if(theIndex<0||theIndex>=listSize)
    {
        cout<<"out of range!"<<endl;
        return;
    }
    //索引有效
    //在表头插入 
    if(theIndex==0)
    {
        //最简单的方法就是用前面类中的构造函数
        firstNode=new chainNode(theElement,firstNode);//太妙了!!!
    }
    else
    {
        //寻找新元素的前驱
        chainNode* p;
        for(int i=0;i<theIndex-1;i++)
        {
            p=p->next;
        }
//        chainNode<T>* addNode=new chainNode<T>(theElement);
//        addNode->next=p->next;
//        p->next=addNode;
        p->next=new chainNode(theIndex,p->next); 
    }
    listSize++;//链表元素个数加一 
}

int main()
{
    chain c(1);
    c.insert(0,1);

    return 0;
}


错误提示是在get函数实现里面的return -1;
哪里有问题呢?
我把return -1注释掉的话就出现错误:[Error] ld returned 1 exit status

  • 写回答

3条回答 默认 最新

  • [PE]经典八炮 2022-01-21 13:03
    关注

    哈哈,返回引用意味着可能修改内部变量,所以和const矛盾,要么改成const int &或者直接int,要么去掉后面的函数修饰符const

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月29日
  • 已采纳回答 1月21日
  • 创建了问题 1月21日

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛