使用 boost::multi_index 时嵌套名称说明符中使用的 C++ 不完整类型

问题描述 投票:0回答:1

我有以下文件:

“声明.hpp”

namespace ns {
    
class Derived;
using DerivedPtr = std::unique_ptr<Derived>;
 
}

“基础.hpp”

namespace ns {

class User;

class Base
{
public:
    uint64_t foo() const;
};

}

“派生.hpp”

namespace ns {

class Derived : public Base
{
public:
    uint64_t bar() const;
};

}

“用户.hpp”

#include "declaration.hpp"
namespace ns {
class User
{
private:
    std::map<uint64_t, DerivedPtr>  map;
};

}

我尝试将

User
中的地图更改为多索引地图,即在我放入的“declaration.hpp”中

struct Foo{};
struct Bar{};
  
using Map = boost::multi_index_container<
        DerivedPtr,
        boost::multi_index::indexed_by<
            boost::multi_index::ordered_unique<boost::multi_index::tag<Foo>,
                boost::multi_index::const_mem_fun<Base, uint64_t, &Base::foo> >,
            boost::multi_index::ordered_non_unique<boost::multi_index::tag<Bar>,
                boost::multi_index::const_mem_fun<Derived, uint64_t, &Derived::bar> > > >;

#include "declaration.hpp"
namespace ns {
class User
{
private:
    Map  map;
};

}

但我明白了

error: incomplete type 'ns::Derived' named in nested name specifier 
。 我无法将
#include "Derived.hpp"
#include "Base.hpp"
放入“declaration.hpp”中 因为由于“Base.hpp”中的前向声明,我得到了
error: member access into incomplete type

有办法实现我想要做的事情吗?是否可以使用索引的固定值,从而消除对成员函数的需要?

c++ boost boost-multi-index
1个回答
0
投票

Derived.hpp
未包含在内,您需要一组 boost 标头。

这有效:

  • 文件

    ./Base.cpp

     #include "Base.hpp"
    
     namespace ns {
         uint64_t Base::foo() const { return 9; }
     } // namespace ns
    
  • 文件

    ./Base.hpp

     #pragma once
     #include <cstdint>
    
     namespace ns {
         class User;
    
         class Base {
           public:
             uint64_t foo() const;
         };
    
     } // namespace ns
    
  • 文件

    ./Derived.cpp

     #include "Derived.hpp"
    
     namespace ns {
         uint64_t Derived::bar() const { return 42; }
     } // namespace ns
    
  • 文件

    ./Derived.hpp

     #pragma once
     #include "Base.hpp"
     namespace ns {
    
         class Derived : public Base {
           public:
             uint64_t bar() const;
         };
    
     } // namespace ns
    
  • 文件

    ./User.cpp

     #include "User.hpp"
    
     namespace ns {
         void User::do_something() {
             map.emplace(std::make_unique<Derived>());
             map.emplace(std::make_unique<Derived>());
         }
     } // namespace ns
    
  • 文件

    ./User.hpp

     #include "Derived.hpp"
     #include "declaration.hpp"
     #include <boost/multi_index/mem_fun.hpp>
     #include <boost/multi_index/ordered_index.hpp>
     #include <boost/multi_index_container.hpp>
     #include <boost/multi_index_container.hpp>
     // #include <map>
    
     namespace ns {
         namespace bmi = boost::multi_index;
         using Map     = boost::multi_index_container<
             DerivedPtr,
             bmi::indexed_by<bmi::ordered_unique<bmi::tag<struct Foo>,                                     //
                                                 bmi::const_mem_fun<Base, uint64_t, &Base::foo>>,          //
                             bmi::ordered_non_unique<bmi::tag<struct Bar>,                                 //
                                                     bmi::const_mem_fun<Derived, uint64_t, &Derived::bar>> //
                             >>;
    
         class User {
           public:
             void do_something();
    
           private:
             // std::map<uint64_t, DerivedPtr> map;
             Map map;
         };
    
     } // namespace ns
    
  • 文件

    main.cpp

     #include "User.hpp"
    
     int main() {
         ns::User user;
    
         user.do_something();
     };
    
© www.soinside.com 2019 - 2024. All rights reserved.