Home > error c2662 > error c2662 const

Error C2662 Const

Contents

here for a quick overview of the site Help Center Detailed answers to any questions you c2662 c++ might have Meta Discuss the workings and policies of this site error c2662 conversion loses qualifiers About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or error c2662 cannot convert 'this' pointer from posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of

Cannot Convert This Pointer From Const To &

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 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' const method : 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 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 through

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 error c2662 cannot convert this pointer from const to conversion loses qualifiers Learn more about Stack Overflow the company Business Learn more about hiring developers

Const_cast Example

or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack

Const Function

Overflow Community 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 error C2662 cannot convert from const to http://stackoverflow.com/questions/12068301/c-error-c2662-cannot-convert-this-pointer-from-const-type-to-type reference up vote 2 down vote favorite this is my first post on stack overflow, and I hope to join the community in the future. I'm in the midst of programming a hash table implementation for an ADT class; most of my methods are up to par within the bounds of the assignment, but this is giving me grief. In this test application which I have been http://stackoverflow.com/questions/20319540/error-c2662-cannot-convert-from-const-to-reference using to test various functions as I write them, I'm recieving the error "error C2662: 'customer::getPhone': cannot convert 'this' ponter from 'const customer' to 'customer &' in reference to the lines "cursor = find_ptr(entry.getPhone());" and "list_head_insert(data[hash(entry.getPhone())], entry);" My code implementation for the functions is as follows: template void table::insert(const RecordType& entry){ node* cursor; cursor = find_ptr(entry.getPhone()); if(cursor == NULL) { list_head_insert(data[hash(entry.getPhone())], entry); ++total_records; } else cursor->set_data(entry); } In this case, getPhone refers to an accessor for an integer private variable, nothing fancy. Finally, for my main test application: #include "Table2.h" #include "Customer.h"; using namespace std; int main () { customer myCustomer( "name", "935 street dr.", 5555555 ); table myTable; cout << myCustomer; myTable.insert(myCustomer); return 0; } The frustrating aspect is that this code is used verbatim in both the text, and in several online examples I've looked up after receiving the compilation error; any help would be greatly appreciated, and I would be happy to clarify anything if need be. I am running VS express 2012 for desktop. c++ const hashtable share|improve this question asked Dec 2 '13 at 1:36 user3055759 133 add a comment| 2 Answers 2 active oldest votes up vote 0 down vote accepted

Forum Visual C++ & C++ Programming Visual C++ Programming [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 &' If this is your first https://forum.qt.io/topic/7774/solved-c2662-function-cannot-convert-this-pointer-from-type1-to-type2 visit, be sure to check out the FAQ by clicking 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 error c2662 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 Mode Switch to Threaded Mode March 6th, 2007,07:07 AM #1 Mybowlcut View Profile View Forum Posts conversion loses qualifiers 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 ITEMLIST): itemList(ITEMLIST) {} inline void ListItems(); private: vector itemList; }; The class member function that the error is occurring in (error line in red): Code: inline void Inventory::ListItems() { if(itemList.empty()) cout << "\n\nYou have no items."; else { vector::const_iterator iter = itemList.begin(); for(iter; iter != itemList.end(); ++iter) { cout << "\n\n"; cout << iter->GetName(); } } } And the class (Item) member function that the iter is trying to access: Code: string GetName() { return name; } I can't get around this error.. it's beyond my knowledge I think. I've tried and I'm seeking helppppppp. If I'm missing anything out let me know! Good judgment is gained from experience. Experience is gained from bad

download a browser that supports JavaScript, or enable it if it's disabled (i.e. NoScript). Home Qt Development General and Desktop C2662: 'function' : cannot convert 'this' pointer from 'type1' to 'type2' C2662: 'function' : cannot convert 'this' pointer from 'type1' to 'type2' This topic has been deleted. Only users with topic management privileges can see it. huckfinn last edited by Hello people, I have a QHash dynamicData which is not const! @ #pragma once #include #include #include #include "DataItem.h" class DataController { public: DataController(void); ~DataController(void); int removeDataItem(qint32 key) const; protected: QHash dynamicData; }; @ And my source @ #include #include "DataController.h" DataController::DataController(void) { // } int DataController::removeDataItem(qint32 key) const { return this->dynamicData.remove( key ) ; } @ I get error C2662: 'QHash::remove' : cannot convert 'this' pointer from 'const QHash' to 'QHash &' My dynamicData is not constant, so remove should be callable. Whats the matter? I dont see the error.. Cheers Huck Reply Quote 0 thisisbhaskar last edited by Your removeDataItem() is const function, and its trying to remove/modify your class member variable. That may be the problem?? Reply Quote 0 JohanSolo last edited by [quote author="Vijay Bhaska Reddy" date="1311172988"]Your removeDataItem() is const function, and its trying to remove/modify your class member variable. That may be the problem??[/quote] It definitely is! A const method cannot modify the current instance (pointed by "this"). You have a conception problem in here : how could a method that removes an item from a container not modify the instance which "owns" the container... Of course there are workaround if you REALLY want the method to be const. You can either declare your QHash mutable or use const_cast to remove the "const" in front of the ‘const QHash’. I would'nt recommend this though. `They did not know it was impossible, so they did it.' -- Mark Twain Reply Quote

 

Related content

c2662 error

C Error table id toc tbody tr td div id toctitle Contents div ul li a href Error C a li li a href Error C a li li a href Error C a li li a href Error C Conversion Loses Qualifiers a li ul td tr tbody table p 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 relatedl this site About Us Learn more about Stack Overflow the p h id Error C p company Business Learn more about hiring developers

error 1 error c2662

Error Error C table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert This Pointer From Const To Conversion Loses Qualifiers a li li a href Const Method a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community relatedl Magazine Forums Blogs Channel Documentation APIs and reference error c conversion loses qualifiers Dev centers Retired content Samples We re sorry The content you requested has error c cannot convert this pointer from

error c2662 msdn

Error C Msdn table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href C a li li a href C Const Function a li ul td tr tbody table p XXX to XXX Visual Studio Languages Windows Desktop Development C Standards Extensions and Interop Question relatedl Sign in to vote Please look at my p h id Error C Conversion Loses Qualifiers p code pragma once class CBox protected double m length double m width double m high error c cannot convert this pointer from

error c2662 volatile

Error C Volatile table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li li a href C Error C a li li a href C Cannot Convert This Pointer From Const a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies relatedl of this site About Us Learn more about Stack Overflow error c conversion loses qualifiers the company Business Learn more about hiring

error c2662 cannot convert this

Error C Cannot Convert This table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href Error C Cannot Convert This Pointer From Const To Conversion Loses Qualifiers a li li a href Const Function a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss relatedl the workings and policies of this site About Us Learn error c cannot convert this pointer from const to more about Stack

error c2662

Error C table id toc tbody tr td div id toctitle Contents div ul li a href Error C a li li a href Error C a li li a href Error C a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups relatedl TechRewards Events Community Magazine Forums Blogs Channel Documentation error c conversion loses qualifiers APIs and reference Dev centers Retired content Samples We re sorry The error c cannot convert this pointer from content you requested has been removed You ll

error c2662 this const

Error C This Const table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href Const Method a li li a href Const cast Example a li li a href Const Function a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards relatedl Events Community Magazine Forums Blogs Channel Documentation APIs c error c cannot convert this pointer from const to and reference Dev centers Retired content Samples We

error c2662 visual studio

Error C Visual Studio table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href C Cannot Convert This Pointer From Const a li li a href Cannot Convert This Pointer From Const To a li li a href C Const Function a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups relatedl TechRewards Events Community Magazine Forums Blogs Channel p h id Error C Conversion Loses Qualifiers p Documentation

error c2662 cannot

Error C Cannot table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li li a href Cannot Convert This Pointer From Const To a li li a href Const cast Example a li li a href Const Correctness a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies relatedl of this site About Us Learn more about Stack Overflow the p h id Error

error c2662 iterator

Error C Iterator p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss relatedl the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of million programmers just like you helping each other Join them it only takes a minute Sign up Visual Studio error C - calling const methods

error c2662 copy constructor

Error C Copy Constructor table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li ul td tr tbody table p Programming Boards C Programming Copy constructor error 'this' pointer conversion Getting started with C or C C relatedl Tutorial C Tutorial C and C FAQ error c conversion loses qualifiers Get a compiler Fixes for common problems Thread Copy constructor error 'this' p h id Error C Cannot Convert this Pointer From p pointer conversion Thread Tools Show Printable Version Email this Page hellip Subscribe to

error c2662 cannot convert

Error C Cannot Convert table id toc tbody tr td div id toctitle Contents div ul li a href Visual Studio Error C a li li a href C Error C a li li a href Const Method a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers relatedl to any questions you might have Meta Discuss the error c cannot convert this pointer from workings and policies of this site About Us Learn more about p h id Visual Studio Error C p Stack Overflow the company Business Learn