1
首先我想显示工作代码,然后解释,我想如何改变事情。这是简单的升压multi_index例如:显式实例提升multi_index容器
//main.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
struct employee
{
int id;
std::string name;
employee(int id, const std::string& name) :id(id), name(name){}
bool operator<(const employee& e)const{ return id<e.id; }
};
typedef boost::multi_index::multi_index_container<
employee,
boost::multi_index:: indexed_by<
// sort by employee::operator<
boost::multi_index:: ordered_unique< boost::multi_index:: identity<employee> >,
// sort by less<string> on name
boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
>
> employee_set;
int main()
{
employee_set es;
es.insert(employee(0, "Bob"));
}
试想一下,如果main.cpp中是另一个模块,无需升压依赖。我想udnerstand如何:
包括与升压多指标容器类被声明前进到main.cpp中 一些头文件中定义我已经试过吨变种的额外.cpp文件 员工的多指标容器,但如果没有这作品。有没有可能创建这样的东西?
//notmain.cpp
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include "notmain.h"
typedef boost::multi_index::multi_index_container<
employee,
boost::multi_index::indexed_by<
// sort by employee::operator<
boost::multi_index::ordered_unique< boost::multi_index::identity<employee> >,
// sort by less<string> on name
boost::multi_index::ordered_non_unique<boost::multi_index::member<employee, std::string, &employee::name> >
>
> employee_set;
现在来h.file我需要填充容器的前向声明(或显式启动)。我可能会误解这些术语,但我对C++很感兴趣并且提升。
//notmain.h
#include <string>
/*
Some how here I need forward declaration or explicit initiation of boost container
class employee_set ???
*/
struct employee
{
int id;
std::string name;
employee(int id, const std::string& name) :id(id), name(name){}
bool operator<(const employee& e)const{ return id<e.id; }
};
这是最终目标。我想提醒一下,main.cpp被设想为另一个模块的.cpp文件,而不依赖于boost。
//main.cpp
#include "notmain.h"
int main()
{
employee_set es;
es.insert(employee(0, "Bob"));
}