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

标签 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;
    }

链表:

#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;
    }
}

最佳答案

你有

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

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

int something = pointer[0];

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

它为您提供了对 linked_list<int> 的引用你最初创建的,你不能分配 linked_listint ,就像错误所说的那样。

pointer是一个指针,pointer[0]不调用 operator[]您在 linked_list 中定义的类(class)。为此,您要么必须使用

int something = (*pointer)[0];

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

就像你想的那样。或者,您可以完全摆脱指针并拥有

linked_list<int> list;

然后你就可以像这样使用它了

list.some_function_call();
int foo = list[some_index];

关于c++ - 链表c++,重载[],整体结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46245658/

相关文章:

java - 为什么 Java 中 LinkedList.add() 的内置实现不将元素添加到 LinkedList 的浅副本,而是自定义实现添加?

c++ - 为什么叫 bool casting?

c++ - 错误 : overloaded 'operator<<' must be a binary operator (has 3 parameters)

oop - 重载的 horzcat() 如何工作?

文本文档中的 C++ 行索引

c++ - 为什么字符串的 find 方法比我的单遍更快?

c++ - 某些情况下 "disappears"的调试功能

Java:LinkedList 上的 QuickSort 给了我异常

C++ - 如何获取结构的最后一个成员类型以及从现有对象访问它的方法?

链表段错误的C++数组