Error C2664 Vector Erase
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 error c2664 cannot convert parameter with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Error C2664 Cannot Convert Parameter 1 From
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: error c2664 in c++ Sign up vector .erase error c2664 up vote 0 down vote favorite I am trying to iterate through a vector and erase specific items from it. I'm working form the end of the vector down so that I don't mess up error c2664 lpcwstr the iterator as items are removed, but when I try to compile it's throwing an error. I looked through some of the other posts with this same error but didn't see anything that applied to my situation, or if there was I didn't catch it, I'm still pretty new to C++ and programming in general. Below is a sample of a simpler code to illustrate my problem. #include #include using namespace std; int vectorErase(vector, int &); int main() { vector test;
Error C2664 Cannot Convert Argument
for(int i=0; i<11;i++) { test.push_back(i); cout< test, int &iterat) { if(test[iterat]>6) { test.erase(iterat); } return 0; } any help would be great c++ vector compiler-errors share|improve this question edited Nov 21 '14 at 15:20 Aleksandar 1,55131032 asked Apr 11 '14 at 19:07 James 219112 3 What is the error? –ChronoTrigger Apr 11 '14 at 19:09 post the error message and specify the line of code that has the error. –atoMerz Apr 11 '14 at 19:09 1 So many things wrong... For one, int& is not an iterator, (which is what erase() takes as argument)! –jrok Apr 11 '14 at 19:16 For starters you need to fix your second loop in main. It should be for(int i=10;i>=0;i--) –jgelderloos Apr 11 '14 at 19:18 add a comment| 4 Answers 4 active oldest votes up vote 2 down vote accepted The most immediate problems in your code are: Passing your vector by value, so the original is never modified. Not using erase() properly. It returns an iterator to the next element in the sequence, which you did not erase (yet). This means if you use iterators and remove an element, you need not (and should not) increment the iterator. An example is forth-coming. In conjunction with the above, simply, you're not using iterators, and you should be. Your code could do without the function and simply do this: #include 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 c2664 cannot convert parameter from const char to lpcwstr 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 error C2664 - code compiles fine in VC6; not in VS 2010 up vote 0 down vote favorite I http://stackoverflow.com/questions/23020726/vector-erase-error-c2664 have a typedef, a class with a member vector using that type and then a method using std::::erase(). #typedef DWORD WordNo_t; class CWordList : public CObject { public: WordNo_t* begin() { return m_Words.begin(); } WordNo_t* end() { return m_Words.end(); } void truncate (WordNo_t *Ptr) { if (Ptr == end()) return; ASSERT (Ptr >= begin() && Ptr < end()); // following line generates C2664 m_Words.erase (Ptr, end()); } private: std:vector m_Words; } The detailed http://stackoverflow.com/questions/3316335/error-c2664-code-compiles-fine-in-vc6-not-in-vs-2010 error is: error C2664: 'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>)' : cannot convert parameter 1 from 'const WordNo_t' to 'std::_Vector_const_iterator<_Myvec>' Pretty new to STL... Any help would be appreciated. c++ stl share|improve this question asked Jul 23 '10 at 8:00 paorear 32 If you're new, grab a good book. –GManNickG Jul 23 '10 at 8:17 add a comment| 2 Answers 2 active oldest votes up vote 2 down vote accepted I'm surprised begin and end are even compiling, they shouldn't. std::vector (and friends) use iterators, not pointers. (Though they are intended to act similarly.) In any case, erase takes an iterator, not a pointer. Because vectors are contiguous, you can make utility functions as such, though: template typename std::vector::iterator to_iterator(T* pPtr, std::vector& pVec) { ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back()); return pVec.begin() + (pPtr- &pVec[0]); } template typename std::vector::const_iterator to_iterator(const T* pPtr, const std::vector& pVec) { ASSERT(pPtr >= &pVec.front() && pPtr <= &pVec.back()); return pVec.begin() + (pPtr - &pVec[0]); } Basically, find out how many elements pPtr is from &pVec[0] (the first element), then add that to pVec.begin(). (Transform the offset from a pointer and the pointer to start into the offset from the start.) This operation is O(1). And then: void truncate (WordNo_t *Ptr
Studio Languages , Windows Desktop Development > C++ Standards, Extensions, and Interop Question 0 Sign https://social.msdn.microsoft.com/Forums/vstudio/en-US/2ea4667b-a4cf-4742-bad4-9da8de04053a/error-c2664stdvectoriteratormyvec-stdvectortyerase?forum=vclanguage in to vote Hi i use vs 2012 ultimate.. how to fix this problem any help? here my code: *.h #define INVALID_UID64 ((UID64_t)~0) struct https://www.daniweb.com/programming/software-development/threads/279832/how-to-change-following-code-to-iterators MSG_CANCEL_ITEM_OK { UID64_t itemUID; }; typedef struct OPENITEM { CItemInfo * pItemBase; int nItemCount; int nItemPrice; int nItemTotal; int nItemIndex; BOOL bRegister; INT error c2664 nBulletCount; OPENITEM() { pItemBase = NULL; nItemCount = 1; nItemPrice = 0; nItemTotal = 0; nItemIndex = 0; bRegister = FALSE; nBulletCount = 0; } ~OPENITEM() { pItemBase = NULL; } } OPEN_BAZAAR_ITEM; class CINFOpen { public: virtual void Recv_DelSellItemOK(MSG_CANCEL_ITEM_OK* pMsg); private: OPEN_BAZAAR_ITEM * FindItem(CItemInfo* pItemInfo); OPEN_BAZAAR_ITEM * error c2664 cannot FindItem(UID64_t itemUID); OPEN_BAZAAR_ITEM * FindItem(int nItemNum, BYTE nItemType = 1, BOOL bRegister = FALSE); protect: // // private: vector m_vecOpenBazaarItem; }; *.cpp void CINFOpen::Recv_DelSellItemOK(MSG_CANCEL_ITEM_OK* pMsg) { OPEN_BAZAAR_ITEM * it = FindItem(pMsg->itemUID); // code blabla if(it) { m_vecOpenBazaarItem.erase(it); //Error c2664:'std::_Vector_iterator<_Myvec> std::vector<_Ty>::erase(std::_Vector_const_iterator<_Myvec>)' : cannot convert parameter 1 from 'OPEN_BAZAAR_ITEM *' to 'std::_Vector_const_iterator<_Myvec>' // if try this m_vecOpenBazaarItem.erase(m_vecOpenBazaarItem.begin() + it); //error C2679: binary '+' : no operator found which takes a right-hand operand of type 'OPEN_BAZAAR_ITEM *' (or there is no acceptable conversion) SetItemInfo( NULL, 0, 0 ); } }; how to fixm_vecOpenBazaarItem.erase(it); pffI'm confused.... Ty!!! Edited by d22xxxx Sunday, August 16, 2015 8:14 PM Sunday, August 16, 2015 8:11 PM Reply | Quote Answers 0 Sign in to vote On 8/16/2015 4:11 PM, d22xxxx wrote: how to fix m_vecOpenBazaarItem.erase(it); pff I'm confused.... You need to make FindItem return an iterator into the vector, not
heywire and gives me 35 errors. all for the erase command. :( for(int i=0; ifront(); j!=answer_->back(); j++) { if(answer_[j]==population_[i][j]) { feedback_[i][j].push_back("Match"); for(int k=tempanswer_->at(i).front(); k!=tempanswer_->at(i).back(); k++) { //find first instance if(tempanswer_[i][k]==answer_[j]) { tempanswer_[i].erase(k); break; } } } } } lotrsimp12345 37 413 posts since Jun 2009 Community Member 3Contributors 8Replies 15Views 6 YearsDiscussion Span 6 Years Ago Last Post by lotrsimp12345 0 Discussion Starter lotrsimp12345 37 6 Years Ago Here's my code when I change it. Get 8 errors for this part alone. :( for(int i=0; ibegin(); individual!=answer_->end(); individual++) { if(answer_->at(individual)==population_->at(i).at(individual)) { feedback_[i][individual].push_back("Match"); for(vector::iterator itr=tempanswer_->at(i).begin(); itr!=tempanswer_->at(i).back(); itr++) { //find first instance if(tempanswer_[i][itr]==answer_[individual]) { tempanswer_[i].erase(itr); break; } } } } } Errors are: Error 1 error C2664: 'const int &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'unsigned int' c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 65 Mastermind Error 2 error C2664: 'const int &std::vector<_Ty>::at(unsigned int) const' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'unsigned int' c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 65 Mastermind Error 3 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 67 Mastermind Error 4 error C2228: left of '.push_back' must have class/struct/union c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 67 Mastermind Error 5 error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\mastermind\mastermind.cpp 68 Mastermind Error 6 error C2679: binary '[' : no operator found which takes a right-hand operand of type 'std::_Vector_iterator<_Ty,_Alloc>' (or there is no acceptable conversion) c:\users...\documents\visual studio 2008\projects\cs 561\mastermind\