不能将“this”指针从“const SqQueue<ElementType>”转换为“SqQueue<ElementType> &
错误 1 error C2662: “int SqQueue<ElementType>::getLength(void)”: 不能将“this”指针从“const SqQueue<ElementType>”转换为“SqQueue<ElementType> &” e:\c++\common\circularsqqueue\circularsqqueue\sqqueue.h 170
错误 2 error C2662: “bool SqQueue<ElementType>::IsEmpty(void)”: 不能将“this”指针从“const MySqQueue<int>”转换为“SqQueue<ElementType> &” e:\c++\common\circularsqqueue\circularsqqueue\mysqqueue.h 25
解析:常量对象使用了非常量成员函数,解决方法是:在该非常量成员函数的声明和定义的参数列表后加上const,使之成为常量成员函数即可
例如:
/*拷贝构造函数*/
template<typename ElementType>
SqQueue<ElementType>::SqQueue(const SqQueue<ElementType>& rightQ) //rightQ是一个常量对象
{ base = NULL;
base = new ElementType[rightQ.getLength()]; //getLength()这个成员函数是一个非常量成员函数
assert(base != NULL);
queueSize = rightQ.queueSize; front = rightQ.front;
rear = rightQ.rear; for (int i = front; i != rear; i = (i + ) % queueSize)
base[i] = rightQ.base[i]; }
解决方法是:在getLength()这个成员函数声明和定义时在它的参数列表后加上const,使之成为一个常量成员函数:
声明时: /*求循环循环顺序队列中元素个数*/
int getLength() const; 定义时:
/*求循环循环顺序队列中元素个数*/
template<typename ElementType>
int SqQueue<ElementType>::getLength() const
{
return (rear - front + queueSize) % queueSize; //不能是(rear - front) % queueSize,否则可能会得出负数
}
不能将“this”指针从“const SqQueue<ElementType>”转换为“SqQueue<ElementType> &的更多相关文章
- 指向const的指针和const指针
1.指向const的指针:const int *p 或 int const *p 解释:p是一个指针,指向const int类型的常量:指针指向的内容为常量,因此不能改变*p的值,但指针p可以改变,指 ...
- C++中的指针与const
刚开始接触C++时,指针和const之间的关系有点混乱,现在总结如下: 一.指向const变量的指针 #include<iostream.h> void main() { const in ...
- C++ Prime:指针和const
与引用一样,也可以令指针指向常量或非常量,类似于常量引用,指向常量的指针不能用于改变其所指对象的值.要想存放常量对象的地址,只能使用指向常量的指针: const double pi = 3.14; / ...
- 指针和Const限定符
指针和Const限定符 1.指向const对象的指针 如果指针指向的是const对象,则不允许使用指针来改变其所指的const值.C++要求指向const对象的指针具有const特性. const d ...
- C++指针与const
在C++中,const修饰符一般用于修饰常量.常量在定义的时候必须初始化,而且值一旦定义之后就不能修改,这样就能保证常量的值在程序运行过程中不会发生变换. 1.指向const对象的指针 指向const ...
- void指针和const指针
void指针:主要是便于传递不同类型的参数 const指针: const char *p :const 在* 的左边表示指向一个常量指针:表示指向的内容不可变(*p不能变,p可以改变) int a = ...
- [C++ Primer Plus] 第7章、函数(一)程序清单——递归,指针和const,指针数组和数组指针,函数和二维数组
程序清单7.6 #include<iostream> using namespace std; ; int sum_arr(int arr[], int n);//函数声明 void ma ...
- 指针和const一些注意事项
1.常量指针(底层const) 指向常量的指针,指针所指向的对象的值无法被修改,若想存放常量对象的地址,只能使用指向常量的指针. 2.指针常量(顶层const) 指针本身是常量,指针本身的值不可修改. ...
- 【C】——指针与const限定符
const限定符和指针结合起来常见的情况有以下几种. const int *a; int const *a; 这两种写法是一样的,a是一个指向const int型的指针,a所指向的内存单元不可改写,所 ...
随机推荐
- Oracle学习(四)_SQL函数
--第一部分:SQL基础 --ch1 简单查询 --ch2 查询基本概念 --ch3 数据过滤 --第二部分:多表操作 --ch4 集合理论 --ch5 内连接 --ch6 外连接 --ch7 子查询 ...
- 20165326 java第七周学习笔记
第七周学习笔记 MySQL(数据管理系统)学习 知识点总结: 不能通过关闭MySQL数据库服务器所占用的命令行窗口来关闭MySQL数据库. 如果MySQL服务器和MySQL管理工具驻留在同一台计算机上 ...
- github咋用昂
github-trend:https://github.com/trending github-usingway:https://zhuanlan.zhihu.com/p/41899093 githu ...
- L267 How to save money
When it comes to saving money, the struggle is all too real. It's like your bank account and your 20 ...
- Flask源码阅读-第三篇(flask\_compat.py)
源码 # -*- coding: utf-8 -*-""" flask._compat ~~~~~~~~~~~~~ Some py2/py3 compatibility ...
- Python异常和异常处理
Python异常和异常处理2017年12月20日 22:17:08 Megustas_JJC 阅读数:114 标签: python 异常处理 更多 个人分类: Python 版权声明:本文为博主原创文 ...
- Python 依赖关系
class Person: def play(self, tools): # 通过参数的传递把另外一个类的对象传递进来 tools.run() print("很开心, 我能玩儿游戏了&quo ...
- Git 创建分支与合并分支
下面以branchName=>aiMdTest为例介绍 1. 下载code git clone masterUrl iva(另存文件名) 2. 创建并切换分支 cd iva git chec ...
- [转]Deep Reinforcement Learning Based Trading Application at JP Morgan Chase
Deep Reinforcement Learning Based Trading Application at JP Morgan Chase https://medium.com/@ranko.m ...
- requestAnimationFrame结束demo