c++ - 错误 C2784,键映射中的类

标签 c++ class dictionary keymaps

容器映射有问题。我需要将我自己的类 Person 存储在键中,但出现错误 C2784(即“编译器无法从提供的函数参数中确定模板参数。”)。这是“Ivor Horton's beginning Visual C++ 2010”一书中的示例

#include<map>
#include<string>
#include <iostream>
using namespace std;

void main()
{
    class Person{
     public:
     string c_name,c_surname;
     Person(string name,string surname){
     c_name=name;
     c_surname=surname;
        }
    };

     map<Person,string> phonebook;

     phonebook.insert(make_pair(Person("Mel","GIBSON"),"24 32 23"));
     phonebook[Person("Mel2","Gibson2")]="243 32 23";

      /* it doesn`t work too
     typedef pair<Person,string> Entry;
     Entry entry1= Entry(Person("Jack","Jones"),"213 567 1234");

     phonebook.insert(entry1);*/

    system("Pause");
}

错误 1 ​​error C2784: 'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem *)': 无法推导出 'const std::basic_string<_Elem 的模板参数,_Traits,_Alloc> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 2 错误 C2784:“bool std::operator <(const _Elem *,const std::basic_string<_Elem,_Traits,_Alloc> &)”:无法从“const”中推断出“const _Elem *”的模板参数main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 3 error C2784:'bool std::operator <(const std::basic_string<_Elem,_Traits,_Alloc> &,const std::basic_string<_Elem,_Traits,_Alloc> &)':无法推断模板'const std::basic_string<_Elem,_Traits,_Alloc> &' 的参数来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 4 error C2784:'bool std::operator <(const std::_Tree<_Traits> &,const std::_Tree<_Traits> &)':无法推断'const std::_Tree 的模板参数<_Traits> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 5 error C2784: 'bool std::operator <(const std::unique_ptr<_Ty,_Dx> &,const std::unique_ptr<_Ty2,_Dx2> &)': 无法推导 'const 的模板参数std::unique_ptr<_Ty,_Dx> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 6 error C2784:'bool std::operator <(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)':无法推断'const std::reverse_iterator 的模板参数<_RanIt> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 7 error C2784:'bool std::operator <(const std::_Revranit<_RanIt,_Base> &,const std::_Revranit<_RanIt2,_Base2> &)':无法推断'const 的模板参数std::_Revranit<_RanIt,_Base> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 8 error C2784: 'bool std::operator <(const std::pair<_Ty1,_Ty2> &,const std::pair<_Ty1,_Ty2> &)' : 无法推导 'const 的模板参数std::pair<_Ty1,_Ty2> &' 来自 'const main::Person' e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 AllClasses

错误 9 error C2676: binary '<' : 'const main::Person' 没有定义此运算符或转换为预定义运算符可接受的类型 e:\microsoft visual studio 10.0\vc\include\xfunctional 125 1 所有类

最佳答案

这里的问题是 std::map 要求您的 key 与 < 相当运算符(operator)。自定义结构/类不是默认的,你需要自定义 operator<进行比较。

关于c++ - 错误 C2784,键映射中的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23246221/

相关文章:

c++ - 在 Rcpp 中选择一个不连续的子矩阵

c++ - 空 vector 占用的空间是否与指向当前设置为 nullptr 的类型的指针一样多?

java - 非法参数异常

c++ - 如何使用字符串和整数获取汽车类的整数结果

python - 将字典传递给构造函数?

Swift 字典默默地不存储新项目

python - 计算 Python 字典或列表使用的总 RAM

c++ - lambda 队列是 C++11 中工作队列的良好设计模式吗?

c++ - 用 `auto` 初始化需要复制构造函数?

java - 考虑到我的用例,创建自定义数据结构的理想方法