boost的问题::multi_index、类型不完整、没有匹配的函数调用等



这是Boost 1.72.0。我做错了什么?这是一个简单的例子,我试图创建一个名为employee的结构,它可以按id和name进行排序。我想在将multi_index集成到一个更大、更复杂的项目中之前尝试一下,因为这个项目需要更多的索引。但我在使用下面的代码时遇到了错误。我使用GCC 7.30编译了它。由于错误消息既复杂又冗长,所以我将其省略

以下是最小示例的完整来源:

#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
using namespace boost::multi_index;
struct employee {
employee(unsigned int id, std::string name)
: id(id), name(name)
{}
unsigned int id;
std::string name;
bool operator<(const employee& other) const noexcept {
return this->id < other.id;
}
};
struct indexedById {};
struct indexedByName {};
using EmployeeContainer = boost::multi_index_container
<
employee,
indexed_by<
ordered_unique<tag<indexedById>, identity<employee>>,
hashed_non_unique<tag<indexedByName>, member<employee, std::string, &employee::name>>
>
>;
int main()
{
EmployeeContainer mi;
auto& inserter = mi.get<indexedByName>();
inserter.emplace(0, "uber duber");
inserter.emplace(1,  "gotcha");
inserter.emplace(3, "dang");
for(auto& it : mi.get<indexedById>())
std::cout << it.id << ": " << it.name << std::endl;
std::cin.get();
return 0;
}

好的,我修复了它。我需要包括头文件和其他文件。

#include <iostream>
#include <string>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/key_extractors.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/member.hpp>
using namespace boost::multi_index;
struct employee {
employee(unsigned int id, std::string name)
: id(id), name(name)
{}
unsigned int id;
std::string name;
bool operator<(const employee& other) const noexcept {
return this->id < other.id;
}
};
struct indexedById {};
struct indexedByName {};
using EmployeeContainer = boost::multi_index_container
<
employee,
indexed_by<
ordered_unique<tag<indexedById>, identity<employee>>,
hashed_non_unique<tag<indexedByName>, member<employee, std::string, &employee::name>>
>
>;
int main()
{
EmployeeContainer mi;
auto& inserter = mi.get<indexedByName>();
inserter.emplace(0, "uber duber");
inserter.emplace(1, "gotcha");
inserter.emplace(3, "dang");
for(auto& it : mi.get<indexedById>())
std::cout << it.id << ": " << it.name << std::endl;
std::cin.get();
return 0;
}

这是输出:

0: uber duber
1: gotcha
3: dang

所以,故事的寓意是,为你正在做的每种类型的索引添加头文件!

最新更新