Home > error c2662 > error c2662 iterator

Error C2662 Iterator

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 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 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Visual Studio: error C2662 - calling const methods up vote 0 down vote favorite EDIT: Silly mistake. Somehow I was using source code files from another folder, for another project, to compile. Was opening the files from Windows explorer and seeing all the consts as they should be, then realised that opening the "same files" from within the Solution Explorer showed different contents. Further checking showed the files associated with the project I was trying to build were not the files I was currently editing. Lesson learned - be more careful with version control. Thanks for the comments, they helped me look down the right lines and stop banging my head off the keyboard looking for a typo. ======================================================== I'm getting multiple "error C2662"s errors in the following code (positions shown in comments): XYVector Rocket::avoidRocketsAndPlanets(float distance, const std::set& otherRockets, const std::set& planets) const { XYVector c = XYVector(); for (std::set::const_iterator it = otherRockets.begin(); it != otherRockets.end(); it++) { if ((it->getPos() - pos).mod() < distance) { c -= (it->getPos() - pos); } } for (std::set::const_iterator it = planets.begin(); it != planets.end(); it++) { XYVector planetPos = it->getPos(); // ^ error C2662: 'Planet::getPos' : cannot convert 'this' pointer from 'const Planet' to 'Planet &' if ((planetPos - pos).mod() < (distance + it->getRadius())) // ^ error C2662: 'Planet::getRadius' : cannot convert 'this' pointer from 'const Planet' to 'Planet &' { c -= (it->getPos() - pos); // ^ error C2662: 'Planet::getPos' : cannot convert 'this' pointer from 'const Planet' to 'Planet &' } } return c; } I don't understand why I'm getting these errors because the methods in question are all declared const. In fact they're declared and defined ident

and the second, error c2664, is on the 3rd param of a for_each algorithm. If anyone can explain these to me I would appreciate it. Here is the code: In class definition: typedef struct tag_CommentKey { string section; // Section where comment appears string key; // key in section where comment appears } COMMENTKEY; typedef struct tag_Comment { int sequence; // Order http://stackoverflow.com/questions/8415688/visual-studio-error-c2662-calling-const-methods of command line (0 for inline, n for stand-alone int position; // Position on line string comments; // Comment text } COMMENTS; typedef multimap m_IniComment; // list of comments typedef set m_CommentSet ; // Some comments COMMENTKEY m_CommentKey; COMMENTS m_CommentText; m_IniComment m_IniComments; // Keyed Comments list m_IniComment::iterator m_itIniComment; m_CommentSet m_Comments; m_CommentSet::iterator m_itComments; void http://www.cplusplus.com/forum/general/2547/ setComments ( COMMENTKEY & commentKey, COMMENTS & commentText ); void getComments ( void ) const; void setCommentSet ( const pair & comment ); and in the implementation: void IniFile::setComments ( COMMENTKEY & commentKey, COMMENTS & commentText ) { m_IniComments.insert (m_IniComment::value_type(commentKey, commentText)); return; } void IniFile::getComments ( void ) const { int number = m_IniComments.count (m_CommentKey); if (number > 0) { if (!m_Comments.empty()) { m_Comments.clear (); // this gets an error C2662! } pair ret = m_IniComments.equal_range (m_CommentKey); for_each (ret.first, ret.second, setCommentSet); // this gets an error c2664 on param 3 } return; } void IniFile::setCommentSet ( const pair & comment ) { m_Comments.insert (m_CommentSet::value_type (comment.second)); return; } Here are the actual errors: --------------------Configuration: Test Ini File - Win32 Debug-------------------- Compiling... IniFile.cpp e:\programming\projects\STL studies\inifile.cpp(763) : error C2662: 'clear' : cannot convert 'this' pointer from 'const class std::set,class std::allocator >' to 'class std::set,class std::allocator > &' Conversion loses qualifiers e:

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 the link http://forums.codeguru.com/showthread.php?416873-RESOLVED-error-C2662-cannot-convert-this-pointer-from-const-Item-to-Item-amp 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 Mode Switch to Threaded Mode error c2662 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 ITEMLIST): itemList(ITEMLIST) {} inline void ListItems(); private: vector itemList; }; The class member function that the error c2662 iterator 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 judgment. Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment? Reply With Quote March 6th, 2007,07:18 AM #2 0xC0000005 View Profile View Forum Posts Senior Member Join Date May 2002 Posts 1,435 Re: error C2662: cannot convert 'this' pointer from 'const Item' to 'Item &' Without seeing more of your code I can't be certain, but maybe this will help: string GetName() const { return name; } It is always good practice to make any class member function that does not modify the contents of the class (such as GetName()) a constant. Reply With Quote March 6th, 2007,07:21 AM #3 kasru View Profile View Forum Posts Junior Member Join Date Feb 2007 Location Voronezh, Russia Posts 19 Re: error C2662: cannot convert 'this' pointer f

 

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 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 const

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

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