Error C2662 Cannot Convert This
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 error c2662 cannot convert 'this' pointer from 'const ' to ' &' more about Stack Overflow the company Business Learn more about hiring developers or
Error C2662 Conversion Loses Qualifiers
posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community c++ error c2662 Stack Overflow is a community of 4.7 million 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 cannot convert this pointer from const to & Type' to 'Type &' up vote 17 down vote favorite 6 I 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*
Error C2662 Cannot Convert This Pointer From Const To Conversion Loses Qualifiers
getAgent(); friend bool operator<(const CombatEvent& lhs, const CombatEvent& rhs) { if (lhs.getType() == rhs.getType()) return true; return 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,74262143 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, yo
Forum Visual C++ & C++ Programming Visual C++ Programming [RESOLVED] error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &' If const method this is your first visit, be sure to check out the FAQ const_cast example by clicking the link above. You may have to register or Login before you can post: click the register
Const Function
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 http://stackoverflow.com/questions/12068301/c-error-c2662-cannot-convert-this-pointer-from-const-type-to-type 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 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 http://forums.codeguru.com/showthread.php?416873-RESOLVED-error-C2662-cannot-convert-this-pointer-from-const-Item-to-Item-amp 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
download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript). Home Qt Development General and Desktop
Programming Boards C++ Programming compile error cannot convert 'this' pointer Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ | Get a compiler | Fixes for common problems Thread: compile error cannot convert 'this' pointer Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode 02-24-2008 #1 George2 View Profile View Forum Posts Registered User Join Date May 2006 Posts 1,579 compile error cannot convert 'this' pointer Hello everyone, Which C++ rule do I break in the following code? Why there is compile error? Code: class Foo{ public: Foo() { } int foo1() { return 100; } const Foo getCurrent1() { return *this; } const Foo& getCurrent2() { return *this; } }; int main() { Foo f; f.getCurrent1().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &' f.getCurrent2().foo1(); // error C2662: 'Foo::foo1' : cannot convert 'this' pointer from 'const Foo' to 'Foo &' return 0; } thanks in advance, George 02-24-2008 #2 tabstop View Profile View Forum Posts and the Hat of Guessing Join Date Nov 2007 Posts 14,185 I got a slightly more helpful warning message from gcc: error: no matching function for call to `Foo::foo1() const' foo1 is not labeled as a const function, therefore it cannot take a const "argument". 02-24-2008 #3 George2 View Profile View Forum Posts Registered User Join Date May 2006 Posts 1,579 Thanks tabstop, Your fix works, cool! I am confused about what the compile outputs, cannot convert 'this' pointer from 'const Foo' to 'Foo &' I think it should change "this" to "*this", right? This pointer is a pointer to class (Foo*) and not class type itself (Foo)? Should be the following? Any comments? cannot convert '*this' from 'const Foo' to 'Foo &' Originally Posted by tabstop I got a slightly more helpful warning message from gcc: error: no matching function for call to `Foo: