Error C2662 Cannot
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the
Error C2662 Cannot Convert 'this' Pointer From
company Business Learn more about hiring developers or posting ads with us Stack Overflow error c2662 conversion loses qualifiers Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7
Cannot Convert This Pointer From Const To &
million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Compile Error C2662 [duplicate] up vote 1 down vote favorite This question already has an answer here: c++ error const method C2662 cannot convert 'this' pointer from 'const Type' to 'Type &' 3 answers I'm trying to pass an object as a reference to a function that accepts the object as a const however the compiler is throwing: error C2662: 'const int DataPacket::GetData(void)': cannot convert 'this' pointer from 'const DataPacket' to 'DataPacket &' IntelliSense says: the object has type qualifiers that are not compatible with the member function object type is: const DataPacket I error c2662 cannot convert this pointer from const to conversion loses qualifiers made a test-cast of the code to demonstrate the issue: #include
Forum Visual C++ & C++ Programming Visual C++ Programming [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &' If this is
Const_cast Example
your first visit, be sure to check out the FAQ by clicking the const function link above. You may have to register or Login before you can post: click the register link above to proceed.
Const Correctness
To start viewing messages, select the forum that you want to visit from the selection below. Results 1 to 11 of 11 Thread: [RESOLVED] error C2662: cannot convert 'this' pointer from 'const http://stackoverflow.com/questions/24815858/compile-error-c2662 Item' to 'Item &' Tweet Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode March 6th, 2007,07:07 AM #1 Mybowlcut View Profile View Forum Posts Visit Homepage Senior Member Join Date Nov 2006 Location Australia Posts 1,569 [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &' Hi guys! http://forums.codeguru.com/showthread.php?416873-RESOLVED-error-C2662-cannot-convert-this-pointer-from-const-Item-to-Item-amp The error: Code: error C2662: 'Item::GetName' : cannot convert 'this' pointer from 'const Item' to 'Item &' My class definition: Code: class Inventory { public: Inventory(vector
return m_Color; } Flower &Flower::operator =(const Flower &f) { m_Color = f.GetColor(); return *this; } The complier tell me error C2662: 'Flower::GetColor' : cannot convert 'this' pointer from 'const Flower' to 'Flower &' I search about error http://www.cplusplus.com/forum/general/15391/ C2662, find that change the function GetColor with end of const: 1
2
3
4
COLOR_ENUM Flower::GetColor(void) https://bytes.com/topic/c/answers/628845-error-c2662-cannot-convert-pointer const { return m_Color; } Then is complier stop telling me error. the question is why error C2662 is about convert 'this' ? I dont try convert 'this'; And funciton 'GetColor()' I must need 'const' even if I not change the class value? Oct 16, 2009 at 3:32am UTC helios (13227) I dont try convert error c2662 'this'Yes, you do. Non-const member functions can only take non-const this parameters. The compiler is not smart enough to look at your function and tell whether or not you're modifying the object. It needs the const there so that it can be sure that the object will not change. Consider that if you had a const int, you couldn't pass it to a function void (int &), because it error c2662 cannot requires that the parameter be non-const. This is exactly what's going on here. Last edited on Oct 16, 2009 at 3:33am UTC Oct 16, 2009 at 4:05am UTC player6 (40) Mmm....I don't understand this: Non-const member functions can only take non-const this parameters. Can you point out 'Non-const member functions' and 'non-const this parameters' in this funciton? Thank you~ Oct 16, 2009 at 4:08am UTC helios (13227) Non-const member function: COLOR_ENUM Flower::GetColor(void); Const member function: COLOR_ENUM Flower::GetColor(void) const; Oct 16, 2009 at 4:15am UTC player6 (40) But why 'this' is const? If it's const, when it become const? Oct 16, 2009 at 8:10am UTC Chewbob (95) It can be const when it does not modify any of its members. 1
2
3
4
5
6
class A { int i; public: int f1() const { return i; } // legal because 'i' is not modified void f2() const { ++i; } // error because it modifies 'i' }; Oct 16, 2009 at 9:19am UTC player6 (40) 'Flower::GetColor' : cannot convert 'this' pointer from 'const Flower' to 'Flower &' Ok, I know, in the error information 'this' is 'f' in m_Color = f.GetColor(); rather than 'this' in return *this; I think I mistake 'this'. Thanks Topic archived. No new replies allowed. C
your question and get tips & solutions from a community of 418,505 IT Pros & Developers. It's quick & easy. error C2662 cannot convert this pointer.. P: 9 mishink7 i am getting this error error C2662: 'std::vector<_Ty>::push_back' : cannot convert 'this' pointer from 'const std::vector<_Ty>' to 'std::vector<_Ty> &' with [ _Ty=std::string ] and [ _Ty=std::string ] and [ _Ty=std::string ] this is my code inside the class customer void customer::add_movie(string move) const { int flag1=0; for (unsigned int i=0; i