简体   繁体   English

C ++模板如何重载运算符和访问私有属性

[英]C++ template how to overloading operator and accessing private property

I am currently trying to implement a simple template-based linked list that takes a generic key/value-pair in C++11. 我目前正在尝试实现一个简单的基于模板的链表,它在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);". 如果我把它放在前向声明中,就像在片段中一样,我似乎无法使用“list.head = new element(ele,nullptr);”​​。 Error: expected type-specifier before 'element' 错误:'element'之前的预期类型说明符

If I put it into the class itself, I can't access list.head even tho it's a friend. 如果我将它放入课堂本身,我就无法访问list.head,即使它是朋友。

You should just leave the declaration of the function template before the class template definition (after the forward declaration), to tell the compiler that operator+= specified in the friend declaration is a template. 您应该在类模板定义之前(在前向声明之后)保留函数模板的声明,以告诉编译器在friend声明中指定的operator+=是模板。 Then define it later. 然后再定义它。 eg 例如

// Forward declarations
template<typename K, typename V>
class list;

// declaration of function template
template<typename K, typename V>
list<K, V> &operator+=(list<K, V> &l, const std::tuple<K, V> ele);

// Class definition
template<typename K, typename V>
class list {
    ...
    friend list<K, V> &operator+=<>(list<K, V> &l, const std::tuple<K, V> ele);
};

// definition of function template
template<typename K, typename V>
list<K, V> &operator+=(list<K, V> &l, const std::tuple<K, V> ele) {
    if (!l.head) {
        // list is empty
        l.head = new typename list<K, V>::element(ele, nullptr);
    }

    return l;
}

PS: PS:

  1. Don't name the parameter with the name list , which conflicts with the name of class template list . 不要使用名称list命名参数,该list与类模板list的名称冲突。

  2. element is a nested struct, in operator+= you should specify it like typename list<K, V>::element . element是一个嵌套的struct,在operator+=你应该像typename list<K, V>::element一样指定它。

  3. Using name list (same as std::list ) is not a good idea. 使用名称list (与std::list相同)不是一个好主意。

LIVE 生活

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

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