1
I am writing an XML interpreter in C++, when I find a bad formation in XML I would like to show the error row and column. I am working with string Iterators and created a function to format the error as follows:
string msgLocalizadaXML(string::const_iterator aItIni, string::const_iterator aItFim, string::const_iterator aItMsg)
{
string linha = "Linha...: " + to_string(count(aItIni, aItMsg, '\n') + 1) + '\n';
constexpr auto numCaracteres = 25;
auto itQuebra = find(reverse_iterator<string::const_iterator> { aItMsg },
reverse_iterator<string::const_iterator> { aItIni },
'\n');
// Linha do erro
string coluna = "Coluna..: " + to_string(distance(itQuebra, aItMsg) + 1) + '\n';
string contexto { max(aItIni, aItMsg - numCaracteres), min(aItFim, aItMsg + numCaracteres) };
replace(contexto.begin(), contexto.end(), '\n', ' ');
contexto = "Contexto: " + contexto + "\n"
" " + string(static_cast<size_t>(distance(max(aItIni, aItMsg - numCaracteres), aItMsg)), '~') + '^';
return linha + coluna + contexto;
}
The error returned GCC 4.8.1:
XMLDocumento.cpp:420:86: error: no matching function for call to 'distance(std::reverse_iterator<__gnu_cxx::__normal_iterator<const char*, std::basic_string<char> > >&, std::basic_string<char>::const_iterator&)'
The error is because the Iterators for the Distance function have to be of the same type, but as I search backwards it returns me a Reverse, as I convert to an iterator?
I tried to build a Std::string::iterator with itQuebra but it didn’t let me.
itQuebra.base()
– pepper_chico
worked out! nor needed to decrement the base to use in the call to Istance
string coluna = "Coluna..: " + to_string(distance(itQuebra.base(), aItMsg) + 1) + '\n';
. Thank you very much!– Nico Engels
@pepper_chico Consider turning this into an answer.
– Guilherme Bernal