链接列表C ,重载[],整体结构
linked list c++, overloading [], overall structure
你好,我写了链接列表,我正在寻找一些建议,我不应该做什么或应该避免什么或我能做些什么,我也遇到了麻烦超载[],我找不到解决方案。
我已声明:
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;
}
}
你有
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]
不调用您在linked_list
类中定义的operator[]
。为此,您要么必须使用
int something = (*pointer)[0];
或
int something = pointer->operator[](0);
就像您想出的那样。或者,您可以完全摆脱指针,并具有
linked_list<int> list;
然后您可以像
一样使用它list.some_function_call();
int foo = list[some_index];
相关文章:
- 在哪里声明结构运算符重载
- 为私有结构定义双参数运算符重载
- 重载嵌套结构/类的流插入运算符
- 逗号在重载 ostream 和 istream 在结构中分开
- 重载运算符>>() 表示未知的输入长度/结构
- 类层次结构中的运算符重载
- 为什么我应该重载 () 运算符以在结构而不是类中priority_queue?
- 具有重载方法的可变参数数据结构
- std::cout 不适用于结构的重载"<<"运算符
- 为什么C++在自定义结构向量时会出现 ostream 重载问题?
- Ho 在标头中声明是结构中的重载运算符?
- 指向结构的指针的运算符重载
- 结构外部的重载运算符
- 无法使用"enable_if"和"is_base_of"将模板函数"operator+"的重载限制到我的类层次结构
- 菱形层次结构中的虚函数重载在 clang 和 gcc 中产生不同的结果
- C++ 无法在 typedef 结构中重载<运算符
- 使用运算符 == 进行结构重载
- 当您可以使用构造函数时,为什么要重载 C++ 类或结构中的 () 运算符(可调用运算符)
- 在结构中重载运算符() C++
- ==运算符使用结构重载