943,553 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1341
  • C++ RSS
Aug 26th, 2009
0

Error: Erasing element from a Vector

Expand Post »
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:
C++ Syntax (Toggle Plain Text)
  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:
Quote ...
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.
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster
gretty is offline Offline
158 posts
since Apr 2009
Aug 26th, 2009
0

Re: Error: Erasing element from a Vector

Use,
C++ Syntax (Toggle Plain Text)
  1. pic_links.erase(pic_links.begin()+i);
Moderator
Reputation Points: 2136
Solved Threads: 1228
Posting Genius
adatapost is offline Offline
6,527 posts
since Oct 2008
Aug 27th, 2009
0

Re: Error: Erasing element from a Vector

... 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
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Moving columns...
Next Thread in C++ Forum Timeline: Can I use MIDI to play a specific frequency





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC