zoukankan      html  css  js  c++  java
  • 不能将“this”指针从“const SqQueue<ElementType>”转换为“SqQueue<ElementType> &

    错误 1 error C2662: “int SqQueue<ElementType>::getLength(void)”: 不能将“this”指针从“const SqQueue<ElementType>”转换为“SqQueue<ElementType> &” e:c++commoncircularsqqueuecircularsqqueuesqqueue.h 170

    错误 2 error C2662: “bool SqQueue<ElementType>::IsEmpty(void)”: 不能将“this”指针从“const MySqQueue<int>”转换为“SqQueue<ElementType> &” e:c++commoncircularsqqueuecircularsqqueuemysqqueue.h 25

    解析:常量对象使用了非常量成员函数,解决方法是:在该非常量成员函数的声明和定义的参数列表后加上const,使之成为常量成员函数即可

    例如:

     1 /*拷贝构造函数*/
     2 template<typename ElementType>
     3 SqQueue<ElementType>::SqQueue(const SqQueue<ElementType>& rightQ)    //rightQ是一个常量对象
     4 {
     5 
     6     base = NULL;
     7     base = new ElementType[rightQ.getLength()];      //getLength()这个成员函数是一个非常量成员函数
     8     assert(base != NULL);
     9     queueSize = rightQ.queueSize;
    10 
    11 
    12     front = rightQ.front;
    13     rear = rightQ.rear;
    14 
    15 
    16     for (int i = front; i != rear; i = (i + 1) % queueSize)
    17         base[i] = rightQ.base[i];
    18 
    19 }

    解决方法是:在getLength()这个成员函数声明和定义时在它的参数列表后加上const,使之成为一个常量成员函数:

     1 声明时:
     2 
     3 /*求循环循环顺序队列中元素个数*/
     4     int getLength() const;
     5 
     6 定义时:
     7 /*求循环循环顺序队列中元素个数*/
     8 template<typename ElementType>
     9 int SqQueue<ElementType>::getLength() const
    10 {
    11     return (rear - front + queueSize) % queueSize;            //不能是(rear - front) % queueSize,否则可能会得出负数
    12 }
  • 相关阅读:
    DEDECMS点击主栏目默认显示第一个子栏目列表的方法
    Dede 删除文档同时文章中的图片的方法
    DEDECMS点击主栏目默认显示第一个子栏目列表的方法
    Inside SharePoint 2010 (5): Pages and Navigation
    开源中文分词FudanNLP
    Caching And Processing 2TB Mozilla Crash Reports In Memory With Hazelcast
    盛大 牛人 blog
    a phd
    lily project
    使用redis实现trie结构
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/9971082.html
Copyright © 2011-2022 走看看