I am currently trying to implement a simple template-based linked list that takes a generic key/value-pair in C++11. Elements should be added to the list by the += operator. The code looks like this:
list
// Forward declarations
template<typename K, typename V>
class list;
template<typename K, typename V>
list<K, V> &operator+=(list<K, V> &list, const std::tuple<K, V> ele) {
if (!list.head) {
// list is empty
list.head = new element(ele, nullptr);
}
return list;
};
// Class definition
template<typename K, typename V>
class list {
private:
struct element {
const K key;
const V value;
element *next;
element(const std::tuple<K, V> tuple, element *ele) :
key(std::get<0>(tuple)),
value(std::get<1>(tuple)),
next(ele) { }
};
element *head = nullptr;
public:
friend list<K, V> &operator+=<>(list<K, V> &list, const std::tuple<K, V> ele);
};
I can't get this to compile. Do I have to put the implementation of the operator into the forward declaration or into the class itself? If I put it in the forward-declaration, like in the snippet, I can't seem to use "list.head = new element(ele, nullptr);". Error: expected type-specifier before ‘element’
If I put it into the class itself, I can't access list.head even tho it's a friend.