Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法
2012-03-21 15:27
615 查看
来源:http://www.cnblogs.com/clever101
下面的一段代码:
#include "stdafx.h"
#include <iostream>
class Point3d
{
public:
Point3d(float x=0.0,float y=0.0,float z=0.0)
:_x(x),_y(y),_z(z)
{
}
float GetX() {return _x;}
float GetY() {return _y;}
float GetZ() {return _z;}
inline std::ostream& operator<<(std::ostream&out)const
{
out<<GetX()<<" "<<GetY()<<" "<<GetZ()<<std::endl;
return out;
}
private:
float _x,_y,_z;
};
复制代码
VS C++2005编译出现下面错误:
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetX”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1> 转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetY”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1> 转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetZ”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
原因在于带const修饰符的接口会把this指针转化为为const this类型。网上一种解决办法是,把需要调用的非const接口都改为const,如上例的GetX、GetY和GetZ函数改为:
float GetX() const{return _x;}
float GetY() const{return _y;}
float GetZ() const{return _z;}
复制代码
我想到的一种办法是可以直接在内部将const修饰符去掉,具体如下:
out<<(const_cast<Point3d*>(this))->GetX()<<" "<<(const_cast<Point3d*>(this))->GetY()<<" "<<(const_cast<Point3d*>(this))->GetZ()<<std::endl;
下面的一段代码:
#include "stdafx.h"
#include <iostream>
class Point3d
{
public:
Point3d(float x=0.0,float y=0.0,float z=0.0)
:_x(x),_y(y),_z(z)
{
}
float GetX() {return _x;}
float GetY() {return _y;}
float GetZ() {return _z;}
inline std::ostream& operator<<(std::ostream&out)const
{
out<<GetX()<<" "<<GetY()<<" "<<GetZ()<<std::endl;
return out;
}
private:
float _x,_y,_z;
};
复制代码
VS C++2005编译出现下面错误:
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetX”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1> 转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetY”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
1> 转换丢失限定符
1>d:\algorithmic\searchnum\console\console.cpp(21) : error C2662: “Point3d::GetZ”: 不能将“this”指针从“const Point3d”转换为“Point3d
&”
原因在于带const修饰符的接口会把this指针转化为为const this类型。网上一种解决办法是,把需要调用的非const接口都改为const,如上例的GetX、GetY和GetZ函数改为:
float GetX() const{return _x;}
float GetY() const{return _y;}
float GetZ() const{return _z;}
复制代码
我想到的一种办法是可以直接在内部将const修饰符去掉,具体如下:
out<<(const_cast<Point3d*>(this))->GetX()<<" "<<(const_cast<Point3d*>(this))->GetY()<<" "<<(const_cast<Point3d*>(this))->GetZ()<<std::endl;
相关文章推荐
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’
- The resolution of ERROR C2662: cannot convert 'this' pointer from "const Class-Type" to "Class&"
- cannot convert 'this' pointer from 'const class Rational' to 'class Rational &' 是什么意思
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- Pointer to non-const type 'PLTexture*'with no explicit ownership错误,解决办法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- 类型转换 -- 使用Unicode下的cannot convert parameter 1 from 'CString' to 'const char *' 解决办法
- 远程登录mysql,报错:message from server: "Host is not allowed to connect to this MySQL server"解决办法
- iOS开发 - "Cast from pointer to smaller type 'int' loses information” 解决办法
- message from server: "Host is not allowed to connect to this MySQL server"解决办法
- mysql数据库 message from server: "Host is not allowed to connect to this MySQL server"解决办法
- message from server: "Host is not allowed to connect to this MySQL server"解决办法
- From an inner class, "this" points to the inner object. How do I get a "this" pointer to the enclosing object?
- System error &H80004015 - This class is configured to run as a security id different from the caller.