'C2447 in a templated class C++
I'm trying to write a class similar to std::list in C++.
It's my first time dealing with templated classes and I get this weird error:
C2447: '{' missing function header (old-style formal list?)
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:
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.
I have read some documentations regarding this error and templated classes but could find what seems to be the error.
Solution 1:[1]
As mentioned in the comments, you will need a typename preceding the declared return type of the function template. This extra 'hint' for the compiler is required in this case because that return type is dependent on the item type; and, quoting from that linked cppreference page, it is "a compound type constructed from a dependent type".
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 }; line; this is now generally accepted as better style, to avoid possible confusion between object initializers and function declarations.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
