鱼C论坛

 找回密码
 立即注册
查看: 2166|回复: 2

[已解决]关联容器之pair问题求解

[复制链接]
发表于 2018-4-25 17:42:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include<iostream>
#include<utility>

using namespace std;

int main()
{
        typedef pair<string,string> Author;
        string tn;
        Author author("LiBai","DuFu");
        if(author.first == "LiBai" && author.second == "DuFu")
                tn =  "ShiRen";
        system("pause");
        return 0;
}

编译报错了 :

1>  正在对“Debug\8_0.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1>  Associative_containers.cpp
1>c:\users\wfd-lzf\documents\visual studio 2010\projects\cpppri\8_0\8_0\associative_containers.cpp(11): error C2678: 二进制“==”: 没有找到接受“std::string”类型的左操作数的运算符(或没有可接受的转换)
1>          可能是“内置 C++ operator==(const char [6], const char [6])”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): 或       “bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): 或       “bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): 或       “bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): 或       “bool std::operator ==(const std::error_code &,const std::error_condition &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): 或       “bool std::operator ==(const std::error_condition &,const std::error_code &)”
1>          尝试匹配参数列表“(std::string, const char [6])”时
1>c:\users\wfd-lzf\documents\visual studio 2010\projects\cpppri\8_0\8_0\associative_containers.cpp(11): error C2678: 二进制“==”: 没有找到接受“std::string”类型的左操作数的运算符(或没有可接受的转换)
1>          可能是“内置 C++ operator==(const char [5], const char [5])”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): 或       “bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): 或       “bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): 或       “bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): 或       “bool std::operator ==(const std::error_code &,const std::error_condition &)”
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): 或       “bool std::operator ==(const std::error_condition &,const std::error_code &)”
1>          尝试匹配参数列表“(std::string, const char [5])”时
1>
1>生成失败。
1>
1>已用时间 00:00:00.92
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========

没有==操作符吗?
最佳答案
2018-4-25 17:52:10
(author.first == "LiBai" && author.second == "DuFu")    first和second返回的不是字符串类型,比较类型错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-4-25 17:52:10 | 显示全部楼层    本楼为最佳答案   
(author.first == "LiBai" && author.second == "DuFu")    first和second返回的不是字符串类型,比较类型错误
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-4-25 18:04:11 | 显示全部楼层
御笔剑客 发表于 2018-4-25 17:52
(author.first == "LiBai" && author.second == "DuFu")    first和second返回的不是字符串类型,比较类型 ...

问题 解决了,不是这里出错,是少了 头文件,多谢!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-10-1 21:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表