2017-12-27 1130 views
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")); 
} 

回答

3

如果類型是類的可見接口的一部分,那麼任何類都依賴的頭必須被包含在內,否則沒有辦法。如果你真的不希望它是可見界面的一部分考慮使用平普爾成語:

公共頭部

#if !defined(MYCLASS_PUBLIC_H_) 
#define MYCLASS_PUBLIC_H_ 

struct MyClassImpl; 
class MyClass { 
    MyClassImpl * pImpl; 
public: 
    void SomeOperation(); 
}; 
#endif 

實施頭:

#if !defined(MYCLASS_IMPL_H_) 
#define MYCLASS_IMPL_H_ 
#include <private_type.h> 
#include "MyClass.h" 
struct MyClassImpl 
{ 
    void Operation(); 

private: 
    SomePrivateType member; 
}; 
#endif 

實現文件:

#include "MyClassImpl.h" 
void MyClass::SomeOperation() 
{ 
    pImpl->Operation(); 
} 

void MyClassImpl::Operation() 
{ 
    // do something with 'member' 
} 

僅看到公共接口的代碼:

#include "MyClass.h" 
void foo() 
{ 
    MyClass inst; 
    inst.SomeOperation(); 
}