[Code::Blocks] 编译错误 vc60中运行正常

酸辣土豆丝 iCS 2016-10-30 04:46:50
#include <iostream>
#include <set>
#include <string>
#include <algorithm>

using namespace std;

// set<string, 二元谓词>
class CCompareStringNoCase
{
public:
// 不区分大小写
bool operator()(const string &str1, const string &str2) const
{
string str1LowerCase;
str1LowerCase.resize(str1.size());
transform(str1.begin(),str1.end(),str1LowerCase.begin(),tolower); //编译 这句错误

string str2LowerCase;
str2LowerCase.resize(str2.size());
transform(str2.begin(),str2.end(),str2LowerCase.begin(),tolower); //这句出错

return (str1LowerCase < str2LowerCase);
}
};

int main()
{
set<string, CCompareStringNoCase> names;

names.insert("Taylor");
names.insert("Jim");
names.insert("tom");
names.insert("Heim");
names.insert("cpp");

set<string, CCompareStringNoCase>::iterator iNameFound = names.find("jim");
if (iNameFound != names.end())
{
cout << "Found: " << *iNameFound << endl;
}
else
{
cout << "Not Found !" << endl;
}


return 0;
}


Code::Blocks 第17行编译错误:
error: no matching function for call to 'transform(std::basic_string<char>::const_iterator, std::basic_string<char>::const_iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)' note: candidates are:|

d:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4940|note: template<class _IIter, class _OIter, class _UnaryOperation> _OIter std::transform(_IIter, _IIter, _OIter, _UnaryOperation)|

d:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4940|note: template argument deduction/substitution failed:|

17|note: couldn't deduce template parameter '_UnaryOperation'|

d:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4977|note: template<class _IIter1, class _IIter2, class _OIter, class _BinaryOperation> _OIter std::transform(_IIter1, _IIter1, _IIter2, _OIter, _BinaryOperation)|

d:\program files (x86)\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\stl_algo.h|4977|note: template argument deduction/substitution failed:|

17|note: candidate expects 5 arguments, 4 provided|

哪位朋友帮忙看看能不能解决了? vc6.0完美通过....
...全文
197 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
终于解决了, 给tolower加个buff就行啦! 感谢楼上的大神们 下面给出完整代码:
#include <iostream>
#include <set>
#include <string>
#include <algorithm>
#include <cctype>  //tolower

using namespace std;

// 不支持c++11, 自定义tolower()
char str_tolower(char c)
{
    return (char)tolower(c);
}

// set<string, 二元谓词>
class CCompareStringNoCase
{
public:
    // 不区分大小写
    bool operator()(const string &str1, const string &str2) const
    {
        string str1LowerCase;
        str1LowerCase.resize(str1.size());
        //std::string::value_type是char, tolower参数是int. 类型不匹配
        transform(str1.begin(),str1.end(),str1LowerCase.begin(),str_tolower);

        string str2LowerCase;
        str2LowerCase.resize(str2.size());
        transform(str2.begin(),str2.end(),str2LowerCase.begin(),str_tolower);

        return (str1LowerCase < str2LowerCase);
    }
};

int main()
{
    set<string, CCompareStringNoCase> names;

    names.insert("Taylor");
    names.insert("Jim");
    names.insert("tom");
    names.insert("HeiM");
    names.insert("cpp");

    set<string, CCompareStringNoCase>::iterator iNameFound = names.find("heim");
    if (iNameFound != names.end())
    {
        cout << "Found: " << *iNameFound << endl;
    }
    else
    {
        cout << "Not Found !" << endl;
    }

    return 0;
}
ipqtjmqj 2016-10-31
  • 打赏
  • 举报
回复
引用 5 楼 asdfghjkl0606 的回复:
[quote=引用 4 楼 ipqtjmqj 的回复:] [quote=引用 2 楼 asdfghjkl0606 的回复:] [quote=引用 1 楼 ipqtjmqj 的回复:] tolower这个函数要声明一下,或者用头文件<cctype>
加了头文件<cctype>还是出错, 不懂怎么声明 [/quote] 楼上说的是对的,原因是类型不匹配,如果编译器不支持c++11的话,就自己定义一个函数[/quote] 确实不支持C++11, 自定义函数是写一个和tolower功能一样的吧: char tolower(char c)这样的[/quote] 名字不能一样
  • 打赏
  • 举报
回复
引用 4 楼 ipqtjmqj 的回复:
[quote=引用 2 楼 asdfghjkl0606 的回复:] [quote=引用 1 楼 ipqtjmqj 的回复:] tolower这个函数要声明一下,或者用头文件<cctype>
加了头文件<cctype>还是出错, 不懂怎么声明 [/quote] 楼上说的是对的,原因是类型不匹配,如果编译器不支持c++11的话,就自己定义一个函数[/quote] 确实不支持C++11, 自定义函数是写一个和tolower功能一样的吧: char tolower(char c)这样的
ipqtjmqj 2016-10-31
  • 打赏
  • 举报
回复
引用 2 楼 asdfghjkl0606 的回复:
[quote=引用 1 楼 ipqtjmqj 的回复:] tolower这个函数要声明一下,或者用头文件<cctype>
加了头文件<cctype>还是出错, 不懂怎么声明 [/quote] 楼上说的是对的,原因是类型不匹配,如果编译器不支持c++11的话,就自己定义一个函数
jiqiang01234 2016-10-30
  • 打赏
  • 举报
回复
std::string::value_type是char,而tolower()的参数是int,类型不匹配。可以用lambda表达式 std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c) { return std::tolower(c); });或者自定义std::transform()第三个参数的仿函数。 另:boost的algortim库中的string子库,有现成的函数 #include <boost/algorithm/string.hpp> std::string str1(" hello world! "); boost::to_upper(str1); // str1 == " HELLO WORLD! "
  • 打赏
  • 举报
回复
引用 1 楼 ipqtjmqj 的回复:
tolower这个函数要声明一下,或者用头文件<cctype>


加了头文件<cctype>还是出错, 不懂怎么声明
ipqtjmqj 2016-10-30
  • 打赏
  • 举报
回复
tolower这个函数要声明一下,或者用头文件<cctype>

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧