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"));
}