C2662 Error
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 C2166
company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions error c2664 Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million error c2662 cannot convert this pointer from programmers, just like you, helping each other. Join them; it only takes a minute: Sign up c++ error C2662 cannot convert 'this' pointer from 'const Type' to 'Type &' up vote 17 down vote favorite 6 I
Error C2440
am trying to overload the c++ operator== but im getting some errors... error C2662: 'CombatEvent::getType' : cannot convert 'this' pointer from 'const CombatEvent' to 'CombatEvent &' this error is at this line if (lhs.getType() == rhs.getType()) see the code bellow: class CombatEvent { public: CombatEvent(void); ~CombatEvent(void); enum CombatEventType { AttackingType, ... LowResourcesType }; CombatEventType getType(); BaseAgent* getAgent(); friend bool operator<(const CombatEvent& lhs, const CombatEvent& rhs) { if (lhs.getType() == rhs.getType()) return true; return
Error C2679
false; } friend bool operator==(const CombatEvent& lhs, const CombatEvent& rhs) { if (lhs.getType() == rhs.getType()) return true; return false; } private: UnitType unitType; } can anybody help? c++ operator-overloading syntax-error friend-function share|improve this question asked Aug 22 '12 at 7:33 thiagoh 1,73262143 add a comment| 3 Answers 3 active oldest votes up vote 36 down vote accepted CombatEventType getType(); needs to be CombatEventType getType() const; Your compiler is complaining because the function is being given a const object that you're trying to call a non-const function on. When a function gets a const object, all calls to it have to be const throughout the function (otherwise the compiler can't be sure that it hasn't been modified). share|improve this answer edited Dec 24 '14 at 3:09 hichris123 6,536113257 answered Aug 22 '12 at 7:36 Hbcdev 4,33522353 add a comment| up vote 5 down vote change the declaration to : CombatEventType getType() const; you can only call 'const' members trough references to const. share|improve this answer answered Aug 22 '12 at 7:36 Andrzej 2,7671225 add a comment| up vote 3 down vote It's a const issue, your getType method is not defined as const but your overloaded operator arguments are. Because the getType method is not guaranteeing that it will not change the class data the compiler is throwing an e
return m_Color; } Flower &Flower::operator =(const Flower &f) { m_Color = f.GetColor(); return *this; } The complier tell me error C2662: error c2511 'Flower::GetColor' : cannot convert 'this' pointer from 'const Flower' to 'Flower
Error C2662 Conversion Loses Qualifiers
&' I search about error C2662, find that change the function GetColor with end of const: cannot convert this pointer from const to & 1
2
3
4
COLOR_ENUM Flower::GetColor(void) const { return m_Color; } Then is complier stop telling me error. the question is why error C2662 is about convert 'this' ? http://stackoverflow.com/questions/12068301/c-error-c2662-cannot-convert-this-pointer-from-const-type-to-type 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 '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 http://www.cplusplus.com/forum/general/15391/ 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 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
Forum Visual C++ & C++ Programming Visual C++ Programming [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &' If this is your first visit, be sure to check out the FAQ by clicking http://forums.codeguru.com/showthread.php?416873-RESOLVED-error-C2662-cannot-convert-this-pointer-from-const-Item-to-Item-amp the link above. You may have to register or Login before you can post: click the register link above to proceed. 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 Item' to 'Item &' Tweet Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid error c2662 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! 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