Reputation: 11
I have this weird error where the code was working before but after some time it stopped compiling. The error is:
Could not find a match for 'std::transform<InputIterator,OutputIterator,UnaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main()
and the lines which it is referring to is:
string ans;
cin>>ans;
std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);
Can someone please help me out as to why this is happening? The includes I used were:
#include <fstream.h>;
#include <iostream.h>;
#include <string>;
#include <time.h>;
#include <vector>;
using namespace std;
Thank you very much
Upvotes: 1
Views: 823
Reputation: 19767
If what you say, that this worked up until very recently, I must assume that someone has introduced a small change elsewhere in the code that breaks things. Now, this works:
#include <string>
#include <algorithm>
#include <cctype>
#include <iterator>
#include <iostream>
int main()
{
std::string s1 {"Hello"}, s2;
std::transform(
std::begin(s1),
std::end(s1),
std::back_inserter(s2),
::tolower);
std::cout << s2 << '\n';
}
I.e. it prints hello
.
If I add these two lines at the top:
#include <locale>
using std::tolower;
I get a similar error to you (not identical). This is because it brings this version of tolower
into scope.
To get back the "proper" version (assuming you did mean the version in the cctype
header?) you can use static_cast
to pick the one you want:
// ...
#include <locale>
using std::tolower;
int main()
{
std::string s1 {"Hello"}, s2;
std::transform(
std::begin(s1),
std::end(s1),
std::back_inserter(s2),
static_cast<int(*)(int)>(::tolower)); // Cast picks the correct fn.
std::cout << s2 << '\n';
}
Edit: I have to say, I'm confused as to why you are picking up that version specifically, rather than getting an ambiguous error. But I can't guess exactly what has been changed in your code...
Upvotes: 2
Reputation: 11730
It works for me. Maybe you forgot to include <algorithm>
.
It should work this way:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
string ans;
cin>>ans;
std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);
cout << ans;
return 0;
}
Upvotes: 0