c++ - 找不到 'std::transform..."的匹配项

标签 c++ templates runtime transform lowercase

我有一个奇怪的错误,代码之前可以运行,但一段时间后它停止编译。 错误是:

Could not find a match for 'std::transform<InputIterator,OutputIterator,UnaryOperation>(char *,char *,char *,charT (*)(charT,const locale &))' in function main() 

它所指的行是:

    string ans;
    cin>>ans;
    std::transform(ans.begin(), ans.end(), ans.begin(), ::tolower);

有人可以帮我看看为什么会这样吗? 我使用的包括:

#include <fstream.h>;
#include <iostream.h>;
#include <string>;
#include <time.h>;
#include <vector>;
using namespace std;

非常感谢

最佳答案

如果如您所说,这直到最近才奏效,我必须假设有人在代码的其他地方引入了一个小改动,从而破坏了一些东西。 现在,这有效:

#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';
}

即它打印 hello。 如果我在顶部添加这两行:

#include <locale>
using std::tolower;

我遇到了与您类似的错误(不完全相同)。这是因为它带来了this version of tolower入范围。 要取回“正确的”版本(假设您指的是 cctype header 中的版本?)您可以使用 static_cast 来选择您想要的版本:

// ...

#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';
}

编辑:我不得不说,我很困惑为什么你要专门选择那个版本,而不是得到一个模棱两可的错误。但我无法准确猜测您的代码中发生了什么变化...

关于c++ - 找不到 'std::transform..."的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17441543/

相关文章:

c++ - 如何封装函数绑定(bind)?

Django:sorl-thumbnail 和 easy-thumbnail 在同一个项目中

css - Wordpress 跳出模板

c++ - 运行时的模板实例化和函数选择

go - 如何在 Go 中访问调用堆栈中的变量

c++ - 避免每帧重新渲染 HUD/GUI 元素

c++ - 如何使用 swig 将 const 字符串参数从 perl 传递到 c++

c++ - 如何为其他目录中的所有 C++ 文件生成标签文件?

c++ - CRTP 类中成员函数的可见性

java - 在同一控制台中从 java 调用并运行 jar 文件