`
tansitongba
  • 浏览: 482217 次
文章分类
社区版块
存档分类
最新评论

Error C2662, cannot convert ‘this’ pointer from ‘const class ’ to ‘class &’的解决办法

 
阅读更多

来源:http://www.cnblogs.com/clever101

下面的一段代码:

#include"stdafx.h"
#include<iostream>

classPoint3d
{
public:
Point3d(floatx=0.0,floaty=0.0,floatz=0.0)
:_x(x),_y(y),_z(z)
{
}

floatGetX(){return_x;}
floatGetY(){return_y;}
floatGetZ(){return_z;}

inlinestd::ostream&operator<<(std::ostream&out)const
{
out<<GetX()<<""<<GetY()<<""<<GetZ()<<std::endl;

returnout;
}

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函数改为:

floatGetX()const{return_x;}
floatGetY()const{return_y;}
floatGetZ()const{return_z;}

我想到的一种办法是可以直接在内部将const修饰符去掉,具体如下:

out<<(const_cast<Point3d*>(this))->GetX()<<""<<(const_cast<Point3d*>(this))->GetY()<<""<<(const_cast<Point3d*>(this))->GetZ()<<std::endl;


分享到:
评论

相关推荐

    c语言之const用法

    c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法 c语言之const用法

    TLKS-HC主板测试软件_open_haah_

    uint8_t const at[] = &quot;AT&quot;;uint8_t const csq[] = &quot;AT+CSQ&quot;;//获取信号强度。uint8_t const cta[] = &quot;AT+CTA=0&quot;;//不休眠uint8_t const cpin[] = &quot;AT+CPIN?&quot;;uint8_t ...

    C++中的模拟class string类的代码 cpp

    class string{ friend int len(string &); friend const string & operator+(const string &s1,const string &s2); friend std::ostream & operator(std::ostream &theStream,const string & str); friend const...

    C++ 模板写的短小字符串类,用于替换字符数组和std::string

    int obj_size() const { return sizeof(*this); } const char* GetData()const { return buffer; } const char* c_str()const { return buffer; } bool operator == (const TinyString& other) const; ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    This separates the implementation from the class definition, while still allowing the implementation to be included where necessary. Another use of -inl.h files is for definitions of function ...

    Test_const_class.rar_class

    Test const class extends Dx Test Case Source Code for Linux.

    我叫MT3bin转XML

    bool SetAttribute( const std::wstring& name, const std::wstring& value); 设置属性 CONode CreateSubElement( const std::wstring& name); 创建子元素 bool InsertText( const std::wstring& text); 设置文本 ...

    cocos2dx 3.4 c++ zip压缩包热更新和解压源码

    void onError(ErrorCode errorCode); void onProgress(int percent); void onSuccess(); void setLuaCallbacks(const std::string& callbackMap); private: std::string *_versionLoadedCallback = nullptr;...

    C语言中const定义常量

    本文章是关于C语言中const定义常量 。

    vb 屏001幕取词程序

    Public Const EM_CHARFROMPOS = &HD7 Public Const EM_GETLINECOUNT = &HBA Public Const EM_GETLINE = &HC4 Public Const EM_LINEINDEX = &HBB Public Const EM_LINELENGTH = &HC1 Declare Sub RtlMoveOptiMemory ...

    移动最小二乘图像变形技术

    / Warp.h: interface for the CWarp class. // ////////////////////////////////////////////////////////////////////// #include #include #include #include #include struct matrix2 { double c11,c12,...

    C++动态数组类模板

    动态数组类模板Array C++ //数组类模板声明 template&lt;class T&gt; class Array { public: Array(int sz = 50);... void Error(ErrorType error, int badIndex = 0)const; //错误处理函数 }; ................

    c语言const的用法详解

    文档用大量的例子介绍了const在c 中的详细用法,看了以后会大有收获!

    方便的实现将程序设为服务

    Public Const ERROR_SUCCESS = 0& Public Const ERROR_NONE = 0 Public Const ERROR_BADDB = 1 Public Const ERROR_BADKEY = 2 Public Const ERROR_CANTOPEN = 3 Public Const ERROR_CANTREAD = 4 Public Const ...

    C语言CONST的使用

    本文是对C语言CONST如何使用的介绍,感兴趣的朋友可以看看。

    火车票售票系统

    +Save const:void +Load ifstream&amp; fin :void &lt;&lt;const&gt;&gt;+ inline Id :int &lt;&lt;const&gt;&gt;+ inline Name :string &lt;&lt;const&gt;&gt;+inline Salary :float &lt;&lt;const&gt;&gt;+...

    C语言的CONST函数调用

    一个较好的介绍CONST数据处理的函数PPT教程。

    c语言const用法小结

    个人整理收集的关于关键字const的用法小结

    Dan_Saks_const_T_vs_T_const

    关于C语言中const的用法。以下摘自原文:In my last column, I discussed one of the reasons why the rules by which a compiler can place data into ROM are a bit more complicated in C++ than they are in C. ...

    C语言中const使用说明

    总结了const的各种使用方法,及其作用效果,谢谢观看

Global site tag (gtag.js) - Google Analytics