I have a problem using the string function erase
with iterators.
The function below takes names of input files and .ini
files and creates the path for an output file. The path is defined as
dir + in_file + def_name + ini_file + ".txt"
I am using erase
to erase the extensions of the input file names.
void Output::vDefault(string in, string ini)
{
//save names
strIn=in;
strIni=ini;
//get working dir
char mydirectory[MAX_PATH] = {""};
GetCurrentDirectory(MAX_PATH,mydirectory);
//erase extensions
strIn.erase(strIn.find_last_of('.'), strIn.end()); // error
strIni.erase(strIni.find_last_of('.'), strIni.end()); // error
//adr starts with folder
strAdr=mydirectory;
//and ends with name
//address=dir+in_file+def_name+ini_file+.txt;
strAdr+=strIn+DEFOUTNAME+strIni+".txt";
}
Running the code causes the error below:
error C2664: 'std::basic_string<_Elem,_Traits,_Ax> &std::basic_string<_Elem,_Traits,_Ax>::erase(unsigned int,unsigned int)' : cannot convert parameter 2 from 'std::_String_iterator<_Elem,_Traits,_Alloc>' to 'unsigned int'
erase
can take two iterators as first and last. Can you help me? I don't know why it does not accept anything but int
in my project.
string::find_last_of()
doesn't return an iterator, so it can't match the iterator overload of string::erase()
.
Fortunately, the string::erase()
overload that takes an index for the position defaults to erasing from there to the end, so the following should work for you:
strIn.erase(strIn.find_last_of('.'));
strIni.erase(strIni.find_last_of('.'));
Of course, the string had better actually have a '.'
character in it...