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 ...
随机推荐
- java7中使用透明时与输入法冲突
在Stackoverflow的这找到了答案,需要设置一下系统参数: static { System.setProperty("sun.java2d.noddraw", " ...
- luogu3941入阵曲
https://www.zybuluo.com/ysner/note/1301562 题面 统计在给出的\(n*m\)矩阵中,有多少个不同的子矩形中的数字之和是\(k\)的倍数? 解析 切不掉这道题是 ...
- bzoj2957 楼房重建——线段树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2957 线段树维护两个值:cnt 能看到的最多楼房数: mx 最大斜率数: 对于一段区间,从左 ...
- FreeMarker:目录
ylbtech-FreeMarker:目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://yl ...
- jsp制作登录正在加载页面.....
1. <body style="margin:0px;"> <div id="loading"> <div class=" ...
- PCB 帆软FineReport安装,布署,配置
公司使用帆软FineReport做为报表平台工具也有一年多时间,而FineReport报表平台与Tomcat Web应用服务是站队在java阵营里,因为相信拥抱微软;.net未来发展会越来越好,所以对 ...
- bzoj 1677: [Usaco2005 Jan]Sumsets 求和【dp】
设f[i]为i的方案数,f[1]=1,考虑转移,如果是奇数,那么就是f[i]=f[i-1]因为这1一定要加:否则f[i]=f[i-1]+f[i>>1],就是上一位+1或者i/2位所有因子乘 ...
- 清北考前刷题day1早安
立方数(cubic) Time Limit:1000ms Memory Limit:128MB 题目描述 LYK定义了一个数叫“立方数”,若一个数可以被写作是一个正整数的3次方,则这个数就是立方数 ...
- Wannafly挑战赛29A御坂美琴
传送门 这套题很有意思2333 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些数,用map记录一下,判断是否所有数都能被分出来就是了 //m ...
- [Swift通天遁地]一、超级工具-(6)通过JavaScript(脚本)代码调用设备的源生程序
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...