I hope this hasn’t been covered in some question before. I looked as best I could, but I think part of the problem in the first place is that I don’t understand what is up, and that may have prevented me from finding a previous answer. My apologies if so, but otherwise…
For practice with templates and generally understanding C++ and code design better, I’ve set to writing a (currently quite simple) implementation of a linked list, mostly seeking to mimic std::list. I’ve been working at implementing iterators properly, and the other components logically, but I’ve run into a snag. I’d guess it is with template syntax somewhere, but I’m not sure. It could be just some silly mistake.
Here’s the general structure of the class:
template <class T>
class LinkedList {
public:
LinkedList();
class Iterator;
void push_front(const T&);
void push_back(const T&);
void pop_front();
void pop_back();
T& front();
T& back();
unsigned int size() const;
bool empty() const;
Iterator begin();
Iterator end();
private:
struct ListNode;
ListNode* m_front;
ListNode* m_back;
unsigned int m_size;
};
template <class T>
class LinkedList<T>::Iterator {
public:
Iterator();
Iterator(const Iterator& rhs);
Iterator(ListNode* const& node);
Iterator operator=(const Iterator& rhs);
T& operator*();
bool operator==(const Iterator& rhs) const;
bool operator!=(const Iterator& rhs) const;
Iterator operator++();
private:
ListNode* m_node;
};
template <class T>
struct LinkedList<T>::ListNode {
T* m_data;
ListNode* m_next;
};
And here is the offending function:
template <class T>
void LinkedList<T>::push_front(const T&) {
if (m_front == NULL) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = NULL;
m_back = m_front;
} else if (m_front == m_back) {
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = m_back;
} else {
ListNode* former_front(m_front);
m_front = new ListNode;
*(m_front->m_data) = T;
m_front->m_next = former_front;
}
}
And the error given by GCC 4.6.3:
linkedlist.hpp: In member function ‘void pract::LinkedList<T>::push_front(const T&)’:
linkedlist.hpp:75:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:80:31: error: expected primary-expression before ‘;’ token
linkedlist.hpp:85:31: error: expected primary-expression before ‘;’ token
I hope all that helps, but if anything else would be desirable please do ask.
Thanks all.
The problems is on these lines:
This is attempting to assign a type to a variable, which is clearly not possible. Probably you want a named argument and to use said argument for this assignment: