链表c ++,重载[],整体结构

时间:2017-09-15 18:37:10

标签: c++ linked-list overloading

您好我写了链表,我正在寻找一些建议,我不应该做什么或者我应该避免什么,或者我能做些什么更好,我也遇到过载问题而且我不能找到解决方案。

我已声明:

    linked_list<int> *pointer=new linked_list<int>();

并填充了一些数字,然后试图通过以下方式与他们联系:

    int something = pointer[0];

等。它没有用,我收到了消息:

    error C2440: 'initializing': cannot convert from 'linked_list<int>' to 'int'

我试过了:

    int something = pointer->operator[](0);

它有效。我不知道出了什么问题。有人可以帮我吗?

其余代码:

Node:

    template <typename variable_type>
    class node {
    private:
        variable_type object;
        node<variable_type> *next_node;

    public:
        node();
        node(const variable_type &new_object, node<variable_type> *new_next_node);
        ~node();
        void set_object(const variable_type &new_object);
        void set_next_node(node<variable_type> *new_next_node);
        variable_type &get_object();
        node<variable_type> *get_next_node();
       };

    #include "node.h"

    template<typename variable_type>
    node<variable_type>::node()
    {
        this->next_node = nullptr;
    }

    template<typename variable_type>
    node<variable_type>::node(const variable_type & new_object, 
        node<variable_type> * new_next_node)
    {
        this->object = new_object;
        this->next_node = new_next_node;
    }

    template<typename variable_type>
    node<variable_type>::~node()
    {
        //
    }

    template<typename variable_type>
    void node<variable_type>::set_object(const variable_type & new_object)
    {
        this->object = new_object;
    }

    template<typename variable_type>
    void node<variable_type>::set_next_node(node<variable_type> * new_next_node)
    {
        this->next_node = new_next_node;
    }

    template<typename variable_type>
    variable_type &node<variable_type>::get_object()
    {
        return this->object;
    }

    template<typename variable_type>
    node<variable_type> *node<variable_type>::get_next_node()
    {
        return this->next_node;
    }

linked_list:

#include "node.cpp"
    #include <iostream>

    template <typename variable_type2>
    class linked_list
    {
    protected:
        node <variable_type2> *head;
        int size;

    public:
    linked_list();
    ~linked_list();
    bool is_empty() const;
    int length() const;
    void insert(variable_type2 &new_object, const int index);
    variable_type2 &remove(const int index);
    void print() const;
    variable_type2 &operator[](const int index);
};

#include "linked_list.h"

template<typename variable_type2>
linked_list<variable_type2>::linked_list()
{
    this->head = nullptr;
    this->size = 0;
}

template<typename variable_type2>
linked_list<variable_type2>::~linked_list()
{
    if (!is_empty()) {
        do {
            node <variable_type2> *temp = this->head;
            this->head = temp->get_next_node();
            delete temp;
        } while (head);
    }
    this->size = 0;
}

template<typename variable_type2>
bool linked_list<variable_type2>::is_empty() const
{
    if (!this->head) {
        return true;
    }
    else {
        return false;
    }
}

template<typename variable_type2>
int linked_list<variable_type2>::length() const
{
    return this->size;
}

template<typename variable_type2>
void linked_list<variable_type2>::insert(variable_type2 &new_object, const int index)
{
    if (this->is_empty()) {
        node <variable_type2> *new_node = new node<variable_type2>(new_object, nullptr);
        this->head = new_node;
    }
    else if (index == 0) {
        node <variable_type2> *new_node = new node<variable_type2>(new_object, this->head);
        this->head = new_node;
    }
    else if (index <= this->length()) {
        node <variable_type2> *temp = this->head;

        for (int i = 1; i < index; ++i) {
            temp = temp->get_next_node();
        }

        node <variable_type2> *new_node = new node<variable_type2>(new_object, temp->get_next_node());
        temp->set_next_node(new_node);
    }
    else { //when index is out of range
        return;
    }

    ++(this->size);
}


template<typename variable_type2>
variable_type2 &linked_list<variable_type2>::remove(const int index)
{
    if (index == 0) {
        node <variable_type2> *node_to_remove = this->head;
        variable_type2 return_object = node_to_remove->get_object();
        this->head = this->head->get_next_node();

        delete node_to_remove;
        --(this->size);
        return return_object;

    }
    else if (index <= this->length()) {
        node <variable_type2> *temp = this->head;

        for (int i = 2; i < index; ++i) {
            temp = temp->get_next_node();
        }

        node <variable_type2> *node_to_remove = temp->get_next_node();
        temp->set_next_node(node_to_remove->get_next_node());
        variable_type2 return_object = node_to_remove->get_object();

        delete node_to_remove;
        --(this->size);
        return return_object;

    }
    else {
        //return;

}

template<typename variable_type2>
void linked_list<variable_type2>::print() const
{
    if (!is_empty()) {
        node <variable_type2> *temp = this->head;

        for (int i = 0; i < this->length(); ++i) {
            std::cout << temp->get_object() << " ";
            temp = temp->get_next_node();
        }
        std::cout << std::endl;
    }
}

template<typename variable_type2>
variable_type2 &linked_list<variable_type2>::operator[](const int index)
{
    if (!is_empty() && index < this->length()) {
        node <variable_type2> *temp = this->head;

        for (int i = 0; i < index; ++i) {
            temp = temp->get_next_node();
        }

        variable_type2 return_value = temp->get_object();
        return return_value;
    }
}

1 个答案:

答案 0 :(得分:4)

你有

linked_list<int> *pointer=new linked_list<int>();

这意味着pointer是一个实际的指针。真实指针在编译器中定义了operator[],它执行指针算术并取消引用结果。这意味着

int something = pointer[0];

相同
int something = *(pointer + 0);

,它为您提供了最初创建的linked_list<int>的引用,并且您无法将linked_list分配给int,就像错误一样。

pointer为指针时,pointer[0]不会调用您在operator[]课程中定义的linked_list。要做到这一点,你必须使用

int something = (*pointer)[0];

int something = pointer->operator[](0);
像你想的那样。或者,你可以完全摆脱指针并拥有

linked_list<int> list;

然后你可以像

一样使用它
list.some_function_call();
int foo = list[some_index];