错误:类型不完整' typename'以嵌套名称说明符命名

时间:2014-11-23 09:07:45

标签: c++ c++11 boost

我尝试定义自己的boost::intrusive::slist 另外,我想在类中有另一个静态方法。 (copy_buffer_from_list)获取列表&和迭代器&作为参数。
问题是一些定义是循环的,我无法做到正确 这是当前的代码:

#include <boost/intrusive/slist.hpp>

typedef boost::intrusive::slist_base_hook<> InputBufferSglHook;

class InputBufferSglNode;

// list
typedef boost::intrusive::slist<InputBufferSglNode, boost::intrusive::cache_last<true>> InputBufferSglList;
typedef InputBufferSglList::iterator InputBufferSglIterator;

class InputBufferSglNode : public InputBufferSglHook
{
public :

    InputBufferSglNode(const void* buffer, size_t size); 

    ~InputBufferSglNode() {};

    // copy 'size_t' bytes from list to target, if list doesn't have enough space, method shall return false
    static bool copy_buffer_from_list(InputBufferSglList &list, InputBufferSglIterator &iter, size_t &offset_in_node, const InputBufferSglIterator& end, uint8_t *target, size_t size);

    void reset();

public:
    const void *m_buffer;
    size_t m_size;
};

// empty list
const InputBufferSglList s_EMPTY_INPUT_BUFF_SGL = InputBufferSglList();

InputBufferSglNode::InputBufferSglNode(const void* buffer, size_t size) :
    InputBufferSglHook(),
    m_buffer(buffer),
    m_size(size)
{
    // empty
}

void InputBufferSglNode::reset()
{
    m_buffer = nullptr;
    m_size = 0;
}

static bool InputBufferSglNode::copy_buffer_from_list(InputBufferSglList &list, InputBufferSglIterator &iter, size_t &offset_in_node, const InputBufferSglIterator& end, uint8_t *target, size_t size)
{
    // implementation
}

注意我试图转发声明InputBufferSglNode,但在编译时遇到以下错误:

error: incomplete type 'InputBufferSglNode' named in nested name specifier
note: in instantiation of template class 'boost::intrusive::slist<InputBufferSglNode, boost::intrusive::cache_last<true>, void, void, void, void>' requested here
typedef InputBufferSglList::iterator InputBufferSglIterator;
        ^
note: forward declaration of 'InputBufferSglNode'
class InputBufferSglNode;
      ^

1 个答案:

答案 0 :(得分:0)

类型不完整,就像它说的那样。这不合法(侵入式容器需要知道实现细节 - 基础,大小等 - 在实例化时)。

文档特别提到在进行这种“嵌套”使用时应避免推断钩子类型。

鉴于此,有几种方法可以解决它:

  1. 你可以随意改变: Live On Coliru

  2. 如果您真的希望InputBufferSglList成为全局typedef,当然,您可以使用别名: Live On Coliru

  3. 更新对于评论,您似乎真的坚持要将此作为前向声明的列表类型,您可以按照in the documentation所述:

    typedef boost::intrusive::slist_base_hook<> InputBufferSglHook;
    class InputBufferSglNode;
    
    // list
    typedef boost::intrusive::slist<
            InputBufferSglNode,
            boost::intrusive::base_hook<InputBufferSglHook>,
            boost::intrusive::cache_last<true> 
        > InputBufferSglList;
    
    typedef InputBufferSglList::iterator InputBufferSglIterator;
    

    工作完成。见 Live On Coliru

  4. 完整演示

    完整的第三个版本,供将来参考:

    #include <boost/intrusive/slist.hpp>
    
    typedef boost::intrusive::slist_base_hook<> InputBufferSglHook;
    class InputBufferSglNode;
    
    // list
    typedef boost::intrusive::slist<
            InputBufferSglNode,
            boost::intrusive::base_hook<InputBufferSglHook>,
            boost::intrusive::cache_last<true> 
        > InputBufferSglList;
    
    typedef InputBufferSglList::iterator InputBufferSglIterator;
    
    class InputBufferSglNode : public InputBufferSglHook
    {
      public:
    
        InputBufferSglNode(const void* buffer, size_t size); 
    
        ~InputBufferSglNode() {};
    
        // copy 'size_t' bytes from list to target, if list doesn't have enough space, method shall return false
        static bool copy_buffer_from_list(InputBufferSglList &list, InputBufferSglIterator &iter, size_t &offset_in_node, const InputBufferSglIterator& end, uint8_t *target, size_t size);
    
        void reset();
    
      public:
        const void *m_buffer;
        size_t m_size;
    };
    
    // empty list
    const InputBufferSglList s_EMPTY_INPUT_BUFF_SGL = InputBufferSglList();
    
    InputBufferSglNode::InputBufferSglNode(const void* buffer, size_t size) :
        InputBufferSglHook(),
        m_buffer(buffer),
        m_size(size)
    {
        // empty
    }
    
    void InputBufferSglNode::reset()
    {
        m_buffer = nullptr;
        m_size = 0;
    }
    
    bool InputBufferSglNode::copy_buffer_from_list(InputBufferSglList &list, InputBufferSglIterator &iter, size_t &offset_in_node, const InputBufferSglIterator& end, uint8_t *target, size_t size)
    {
        // implementation
    }
    
    int main() {
    }