std::toupper
is an overloaded function; that’s why you’re getting <unresolved overloaded function type>
in the error message. To select a particular overload, you need to cast it:
static_cast<int(*)(int)>(std::toupper)
for_each
is also not the right choice for this task—it will call toupper
for each string in the list, then discard the result. std::transform
would be the appropriate choice—it writes its output to an output iterator. However, toupper
works on characters, not strings. You could still use transform
to call toupper
for each character in a string:
std::transform(
a_string.begin(),
a_string.end(),
a_string.begin(),
static_cast<int(*)(int)>(std::toupper)
);
It would probably be clearer in this simple case to use loops:
for (TVector::iterator i = a_list.begin(), end = a_list.end(); i != end; ++i) {
for (std::string::size_type j = 0; j < i->size(); ++j) {
(*i)[j] = toupper((*i)[j]);
}
}
But if you wanted to write it with <algorithm>
and <iterator>
tools only, you could make a functor:
struct string_to_upper {
std::string operator()(const std::string& input) const {
std::string output;
std::transform(
input.begin(),
input.end(),
std::back_inserter(output),
static_cast<int(*)(int)>(std::toupper)
);
return output;
}
};
// ...
std::transform(
a_list.begin(),
a_list.end(),
a_list.begin(),
string_to_upper()
);