定义multi_index_container ordered_non_unique时遇到问题

Trouble defining multi_index_container ordered_non_unique

本文关键字:unique 遇到 non 问题 ordered multi index container 定义      更新时间:2023-10-16

我正在玩一些boost容器,但我最近遇到了一个封锁,因为我似乎不能正确定义multi_index_container。我下面是一个例子,我抓住离线,但它仍然给我一个错误信息:

struct boost::multi_index::global_fun<const node&, int, <error-constant>>
Error: Expression must have a constant value

下面是我的声明:

#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE
#include <boost/config.hpp>
#include <string>
#include <iostream>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
using namespace boost::multi_index;
struct node
{
    node(std::string da, int in) {
        data = da;
        numerical = in;
    };
    std::string data;
    int numerical;
};
int main()
{
    typedef multi_index_container<
        node,
        indexed_by<
            hashed_unique<
                member<node,std::string, &node::data>>,
            ordered_non_unique<
                global_fun<const node&, int, node::numerical>> //right here, the value numerical errors
            >
        > node_type;

}

我有一种预感,我没有包括一个文件,但我找不到一个解决方案。

应该这样做:

typedef multi_index_container<
  node,
  indexed_by<  hashed_unique< member<node,std::string, &node::data> >
             , ordered_non_unique< member<node, int, &node::numerical> >
            >
  > node_type;

global_fun期望一个全局函数。&node::numerical&node::data一样是成员。当然,您可以编写一个接受节点并提取它的函数,但为什么要这样做呢?

您还错过了member.hpp包含