简体   繁体   English

模板中的 C2447 class C++

[英]C2447 in a templated class C++

I'm trying to write a class similar to std::list in C++.我正在尝试编写类似于 C++ 中的std::list的 class。

It's my first time dealing with templated classes and I get this weird error:这是我第一次处理模板类,我得到了这个奇怪的错误:

C2447: '{' missing function header (old-style formal list?) C2447:“{”缺少 function header(旧式正式列表?)

Here is the code snippet:这是代码片段:

template<typename item>
mylist<item>::iterator mylist<item>::erase(iterator pos)
{
    iterator cursor(head);
    while (cursor->next != (*pos)) ++cursor;
    if ((*cursor) == nullptr || (*cursor) == head) return iterator(nullptr);
    m_node* tmp = cursor->next->next;
    delete cursor->next;
    cursor->next = tmp;
    --m_size;
    return tmp;
}

The class definition: class定义:

template<typename item>
class mylist
{
    class m_node
    {
        friend class mylist;
        item* node_data;
        m_node* next;
    public:
        m_node()
        {
            next = nullptr;
            node_data = nullptr;
        }
        m_node(const item& ITEM): m_node()
        {
            node_data = new item(ITEM);
        }
        
        ~m_node() { delete node_data; delete next; }
    };
     m_node* head;
     m_node* tail;
     unsigned int m_size;
public:
    class iterator
    {
        friend class mylist;
        m_node* m_ptr;
    public:
        iterator()
        {
            m_ptr = nullptr;
        }
        iterator(m_node* i_ptr)
        {
            m_ptr = i_ptr;
        }
        iterator(const iterator& other)
        {
            m_ptr = other.m_ptr;
        }
        ~iterator(){}
        const iterator& operator=(const iterator& other) const
        {
            m_ptr = other.m_ptr;
            return this;
        }
        iterator& operator++()
        {
            ++m_ptr;
            return *this;
        }
        item operator->()
        {
            return *(m_ptr->node_data);
        }
        item operator*()
        {
            return *(m_ptr->node_data);
        }
        bool operator!=(const iterator& other) { return m_ptr != other.m_ptr; }
        bool operator ==(const iterator& other) { return m_ptr == other.m_ptr; }
    };
    mylist()
    {
        head = tail = new m_node();
        m_size = 0;
    }
    ~mylist()
    {
        delete head;
    }
    bool isempty() const
    {
        return head == tail;
    }
    const iterator& push_back(const item& i_item)
    {
        tail->next = new m_node(i_item);
        tail = tail->next;
        ++m_size;
        return iterator(tail);
    }
    iterator begin() const
    {
        return iterator(head);
    }
    iterator end() const
    {
        return nullptr;
    }
    item& back()
    {
        return *(tail->node_data);
    }
    unsigned int size() const
    {
        return m_size;
    }
    iterator erase(iterator pos);
    void remove(item T);
};

The error occurs at the first curly of the function's scope.错误发生在函数 scope 的第一个卷曲处。

I have read some documentations regarding this error and templated classes but could find what seems to be the error.我已经阅读了一些有关此错误和模板化类的文档,但可以找到似乎是错误的地方。

As mentioned in the comments, you will need a typename preceding the declared return type of the function template.正如评论中提到的,您需要在typename模板声明的返回类型之前有一个类型名。 This extra 'hint' for the compiler is required in this case because that return type is dependent on the item type;在这种情况下,编译器需要这个额外的“提示”,因为返回类型取决于item类型; and, quoting from that linked cppreference page, it is "a compound type constructed from a dependent type" .并且,引用该链接的 cppreference 页面,它是“从依赖类型构造的复合类型”

template<typename item>
typename mylist<item>::iterator mylist<item>::erase(iterator pos)
{
    iterator cursor{ head };
    while (cursor->next != (*pos)) ++cursor;
    if ((*cursor) == nullptr || (*cursor) == head) return iterator(nullptr);
    m_node* tmp = cursor->next->next;
    delete cursor->next;
    cursor->next = tmp;
    --m_size;
    return tmp;
}

Also note that I have changed the parentheses to curly braces in the iterator cursor{ head };另请注意,我已将iterator cursor{ head }; line;线; this is now generally accepted as better style, to avoid possible confusion between object initializers and function declarations.这现在被普遍认为是更好的风格,以避免 object 初始化器和 function 声明之间可能发生的混淆。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM