Error: Erasing element from a Vector

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2009
Posts: 150
Reputation: gretty is an unknown quantity at this point 
Solved Threads: 7
gretty gretty is offline Offline
Junior Poster

Error: Erasing element from a Vector

 
0
  #1
Aug 26th, 2009
Hello

I have a function that writes some text to a text file.

My Problem is: At one point I try to erase an element from a vector ,but I get an error & I dont know why, it looks sytaxically (is that a word? ) correct & I am including the correct libraries (vector, string etc)

function:
  1.  
  2. // pic_links is a private vector of the class generate_post
  3. void generate_post::delete_used_pics(char* file) {
  4.  
  5. int match=0;
  6. ofstream outfile;
  7.  
  8. outfile.open(file);
  9.  
  10. if (!outfile) {
  11. cout << "Failed to open file";
  12. }
  13. for (int i=0; i<(int)pic_links.size(); i++) {
  14. for (int j=0; j<6; j++) {
  15. if (pic_links.at(i) == post[j]) {
  16. match++;
  17. pic_links.erase(pic_links.at(i)); // the error occurs here
  18. }
  19. }
  20. if (match==0) {
  21. outfile << "http://inspic.com" << pic_links.at(i) << "\n";
  22. }
  23. match=0;
  24. }
  25.  
  26. outfile.close();
  27. }

error:
no matching function for call to `std::vector<std::string, std::allocator<std::string> >::erase(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >&)'
Last edited by gretty; Aug 26th, 2009 at 6:35 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Error: Erasing element from a Vector

 
0
  #2
Aug 26th, 2009
Use,
  1. pic_links.erase(pic_links.begin()+i);
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: Error: Erasing element from a Vector

 
0
  #3
Aug 27th, 2009
... and perform the outer loop in reverse direction to avoid problems with overlooking the next element after erasing or index going out of bounds when erasing the last element
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 417 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC