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:

// pic_links is a private vector of the class generate_post
void generate_post::delete_used_pics(char* file) {

	int match=0;
	ofstream outfile;

	outfile.open(file);

	if (!outfile) {
		cout << "Failed to open file";
	}
	for (int i=0; i<(int)pic_links.size(); i++) {
		for (int j=0; j<6; j++) {
			if (pic_links.at(i) == post[j]) {
				match++;
		pic_links.erase(pic_links.at(i)); // the error occurs here
			}
		}
		if (match==0) {
			outfile << "http://inspic.com" << pic_links.at(i) << "\n";
		}
		match=0;
	}

	outfile.close();
}

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> >&)'

Use,

pic_links.erase(pic_links.begin()+i);
Member Avatar for jencas

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.