C++ Error C2662 cannot convert 'this' pointer from 'const *'
---恢复内容开始---
这个错误在于一点:常量对象只能调用常量成员(函数\变量),不能调用非常量成员。另一方面,非常量对象,既可以调用常量成员,又可以调用非常量成员。
class A {
public:
void fun_1() {
std::cout << "非常量函数" << std::endl;
}
void fun_2() const {
std::cout << "非常量函数" << std::endl;
}
};
int main() {
const A a;
a.fun_1();//编译报错,
a.fun_2();//编译通过
}
上面这个简单得代码,可以说明这个问题。
对于实例化对象a, 被定义为常量对象,因此可以调用调用fun_2(), 不能调用fun_1()
在C++当中,我们在函数参数后面加上const,如上面代码fun_2()那样,则将整个成员变成常量成员,即将*this, 转换成const *this.
C++ Error C2662 cannot convert 'this' pointer from 'const *'的更多相关文章
- C++中的error C2662,const的this指针问题
今天在写C++代码的时候遇到一个错误,涉及到了常量的this指针的问题. 简化后的代码如下: #include <iostream> #include <string> usi ...
- cannot convert from pointer to base class 'QObject' to pointer to derived class 'subClass' via virtual base 'baseClass'
QT 编译不过的另一个问题: 1. 新建一个console工程 QT -= gui CONFIG += c++ console CONFIG -= app_bundle # The following ...
- Spring Boot + Bootstrap 出现"Failed to decode downloaded font"和"OTS parsing error: Failed to convert WOFF 2.0 font to SFNT"
准确来讲,应该是maven项目使用Bootstrap时,出现 "Failed to decode downloaded font"和"OTS parsing error: ...
- Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer
Heka 编译安装后 运行报错 panic: runtime error: cgo argument has Go pointer to Go pointer 解决办法: 1. Start heka ...
- ** Error in `./g2o_viewer': realloc(): invalid pointer:
问题: defe@defe-Precision-Tower-3620:~/project/Demo/UseG2OforPoseGraph/useg2oforposegraph$ ./g2o_viewe ...
- 浏览器警告Failed to decode downloaded font和OTS parsing error: Failed to convert *** font to ***
昨晚,在做一个自己感兴趣的东西时,发现浏览器报警告,Failed to decode downloaded font以及OTS parsing error: Failed to convert *** ...
- c/c++ 重载 数组 操作符[] operator[ is ambiguous, as 0 also mean a null pointer of const char* type.
// Note: //int x = a[0].GetInt(); // Error: operator[ is ambiguous, as 0 also mean a null pointer of ...
- OpenGL与vs编程——error C2440: “glMaterialfv”: 无法从“GLfloat”转换为“const GLfloat *”
void setMaterial(const GLfloat mat_diffuse[4],GLfloat mat_shininess){static const GLfloat mat_specul ...
- error C2664: 'TextOutW' : cannot convert parameter 4 from const char [5]' to LPCTSTR
转自:http://blog.sina.com.cn/s/blog_4aa4593d0100odra.html 问题的原因是字符串ANSI和Unicode编码的区别, VC6与VS2003等默认使用A ...
随机推荐
- 使用MyBatis Generator自动生成MyBatis的代码
这两天需要用到MyBatis的代码自动生成的功能,由于MyBatis属于一种半自动的ORM框架,所以主要的工作就是配置Mapping映射文件,但是由于手写映射文件很容易出错,所以可利用MyBatis生 ...
- 【HNOI模拟By YMD】move
Description 设P(n)为从(0,0)移动到点(n,0)的不同路径数目,移动的方式有以下三种:(x,y)->(x+1,y-1),(x,y)->(x+1,y),(x+y)-> ...
- initial ram disk
1 什么是initial ram disk 它就是一个做好了的文件系统,其存储空间是ram.在kernel启动的第一个阶段,会被mount成根文件系统. 2 为什么需要initial ram disk ...
- TQ210--UBOOT移植笔记--添加自己的单板【学习笔记】
在uboot的源码的根目录下的readme中有介绍如何在uboot中添加自己的单板: 一.在boards.cfg中添加自己的单板的信息,可以模仿smdkc100去添加自己的单板的信息 二.复制单板的配 ...
- MAMP/xampp安装redis
nmp/amp/xampp安装redis 一.安装redis服务 1.通过homebrew安装redis sudo brew install redis 2.启动redis服务,且接受客户端连接 su ...
- bzoj 2428 均分数据
题目大意: 已知N个正整数 将它们分成M组,使得各组数据的数值和最平均,即各组的均方差最小 求最小均方差 思路: 模拟退火 #include<iostream> #include<c ...
- CSS左侧固定右侧自适应
方法一: float + margin.left{ float: left; width: 100px; }.right {margin-left:100px; } /*清除浮动*/ .contain ...
- [BZOJ5109/CodePlus2017]大吉大利,晚上吃鸡!
Description 最近<绝地求生:大逃杀>风靡全球,皮皮和毛毛也迷上了这款游戏,他们经常组队玩这款游戏.在游戏中,皮皮和毛毛最喜欢做的事情就是堵桥,每每有一个好时机都能收到不少的快递 ...
- [NOIP2004]火星人
Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数 ...
- 程序 从存储卡 内存卡 迁移到 SD卡
程序 从存储卡 内存卡 迁移到 SD卡 如果你想移动其他软件,在应用市场界面,点击“管理 > 应用搬家”,点击需要转移的应用旁边的“移至SD卡”即可.