2017-12-27 1128 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(); 
}