Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;STL错误的键类型_C++_Stl_Types_Map_Key - Fatal编程技术网

C++ C++;STL错误的键类型

C++ C++;STL错误的键类型,c++,stl,types,map,key,C++,Stl,Types,Map,Key,无法理解这一点:g++编译器对: lengths.insert(pair<Deux,long>(d,one)); 还使用了结构节点: struct Node { long name; long guest; map <long,Node*>nodes; /*bool operator<(const Node& node)const{ if ((*this).name<node.name) return true; if ((*t

无法理解这一点:g++编译器对:

lengths.insert(pair<Deux,long>(d,one));
还使用了结构节点:

struct Node {
   long name;
   long guest;
   map <long,Node*>nodes;
/*bool operator<(const Node& node)const{
 if ((*this).name<node.name) return true;
 if ((*this).name>node.name) return false;
  return (*this).guest<(*this).guest;
}*/
struct节点{
长名称;
长客;
地图节点;
/*bool操作符path::Root))->Node::nodes.std::map::end
[with _Key=long int,_Tp=Node*,_Compare=std::less,_Alloc=std::allocator]()

编译器抱怨没有
运算符编译器抱怨没有
运算符我猜您的结构缺少一个
compare
函数,用于将
Deux
对象作为键进行内部排序。在映射中,必须对它们进行排序

这是
std::map

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;
template类映射;
因此,如果您定义这样的函数:

bool operator < (Deux const & d1, Deux const & d2)
{
   if(d1.big > d2.big)
      return false;
   if(d1.big < d2.big)
     return true;
   return d1.small < d2.small;  
}
bool compare_deux(Deux& a, Deux& b) {
    return a.big < b.big;
}
// must be in same namespace as Deux
bool operator< (Deux const &lhs, Deux const &rhs) {
    return lhs.foo < rhs.foo;
}
bool compare_deux(deux&a,deux&b){
返回a.big
并将其作为模板参数传递:

map <Deux, long, compare_deux> lengths;
map长度;

你应该没事。

我猜你的结构缺少一个
compare
函数,该函数将
Deux
对象作为键进行内部排序。在映射中,必须对它们进行排序

这是
std::map

template < class Key, class T, class Compare = less<Key>,
           class Allocator = allocator<pair<const Key,T> > > class map;
template类映射;
因此,如果您定义这样的函数:

bool operator < (Deux const & d1, Deux const & d2)
{
   if(d1.big > d2.big)
      return false;
   if(d1.big < d2.big)
     return true;
   return d1.small < d2.small;  
}
bool compare_deux(Deux& a, Deux& b) {
    return a.big < b.big;
}
// must be in same namespace as Deux
bool operator< (Deux const &lhs, Deux const &rhs) {
    return lhs.foo < rhs.foo;
}
bool compare_deux(deux&a,deux&b){
返回a.big
并将其作为模板参数传递:

map <Deux, long, compare_deux> lengths;
map长度;

您应该没事。

您没有提到错误消息。请始终发布它

因此,我需要将我的文章分成两个不同的部分

遗失申报单。 这是因为
Deux
在声明
map
时是未知的

您需要在
map
之前声明
Deux
,因为
map
需要其参数的完整定义

缺少比较器。 如果需要访问非公共成员,可以将其设置为成员功能:

bool operator< (Deux const &rhs) {
    return this->foo < rhs.foo;
}
bool运算符<(双数常量和右数){
返回此->foo
如果这种比较是武断的,就按照康斯坦丁尼乌斯的建议去做


下一次
您可以通过发布实际代码或最小测试用例以及提及错误消息来节省我们的时间。

您没有提及错误消息。请始终发布它

因此,我需要将我的文章分成两个不同的部分

遗失申报单。 这是因为
Deux
在声明
map
时是未知的

您需要在
map
之前声明
Deux
,因为
map
需要其参数的完整定义

缺少比较器。 如果需要访问非公共成员,可以将其设置为成员功能:

bool operator< (Deux const &rhs) {
    return this->foo < rhs.foo;
}
bool运算符<(双数常量和右数){
返回此->foo
如果这种比较是武断的,就按照康斯坦丁尼乌斯的建议去做


下一次 您可以通过发布您的实际代码或一个最小的测试用例,以及提到错误消息来节省我们的时间。

使用

length.insert(形成一对(d,一));
使用

length.insert(形成一对(d,一));

为了添加另一个回复,下面是我如何实现严格排序的
Deux
。我们只使用字典排序:

struct Deux
{
  long big, small;

  inline bool operator<(const Deux & o) const
  {
    return big < o.big || (!(o.big < big) && small < o.small);
  }
};

std::map<Deux, T> m; // works now!
struct Deux
{
长的大,小的;

内联布尔运算符只是为了添加另一个回复,下面是我如何实现严格排序的
Deux
。我们只使用字典排序:

struct Deux
{
  long big, small;

  inline bool operator<(const Deux & o) const
  {
    return big < o.big || (!(o.big < big) && small < o.small);
  }
};

std::map<Deux, T> m; // works now!
struct Deux
{
长的大,小的;

内联布尔运算符您需要声明<运算符或在创建映射时传递比较函数

struct Deux     
{
  long big; 
  long small

  bool operator < (const Deux &n) const
  {         
    if(big != n.big)
       return big < n.big;
    else
       return small < n.small;
   }
};
struct Deux
{
长大;
长小
布尔运算符<(常数为双数和n)常数
{         
如果(大!=n.big)
返回大
您需要声明<运算符或在创建地图时传递比较函数

struct Deux     
{
  long big; 
  long small

  bool operator < (const Deux &n) const
  {         
    if(big != n.big)
       return big < n.big;
    else
       return small < n.small;
   }
};
struct Deux
{
长大;
长小
布尔运算符<(常数为双数和n)常数
{         
如果(大!=n.big)
返回大
编译器错误消息是什么?为什么不更新您的问题以向我们显示实际的代码和实际的编译器错误消息?抱歉,我的internet连接中断。好的,谢谢,此重载运算符的问题operator@Oli不,不仅仅是你:)@Oli:这是一个正确的观点,但我个人更喜欢重载
操作符好的,谢谢,所以我需要这样的东西:booloperator@loldop:我认为我的版本更正确,因为它处理了大的等于+1:How的情况我一直在努力避免超载
operator@Oli不,这不仅仅是你:)@Oli:这是一个有效的观点,但我个人更喜欢重载
运算符,除了模板参数是函数类型,而不是函数指针。
compare_deux