链表的重载运算符+
Overloading operator+ for linked lists
我是初学者,现在我正在尝试实现包含函数begin((的类链表。 该函数很好地返回列表中的第一个元素,但我试图做的是在下一个位置返回迭代器,例如如下所示:
List<int>::iterator iter2 = a.begin() + 2; // or iter2 = iter2 + 1;
cout <<iter2->data;
其中输出是垃圾,如21213123..
所以在这里我想我应该使用运算符重载+,这是我的函数:
template<class T>
Node<T>* operator+(const Node<T>& iter, const int& pos)
{
cout << "in"; for testing, but seems that doesnt even entry here
return NULL;
}
那么谁能帮我?谢谢
PS:这是类节点
template<class T>
class Node {
public:
T data;
Node* next;
Node() :data(0), next(NULL) {}
Node(T val, Node<T>* pointer = NULL) :data(val), next(pointer) {}
};
和列表类
template<class T>
class List {
public:
typedef Node<T>* iterator;
typedef const Node<T>* const_iterator;
//constructors
List() { item = NULL; counter = 0; }
explicit List(int val) :counter(1) { item = new Node<T>(val); }
~List() { // to be made
}
//public functions
int size() { return counter; }
iterator begin() {
return item;
}
iterator end()
{
iterator last = item;
while (last->next != NULL)
{
last = last->next;
}
return last;
}
void push_front(const int& val) {
iterator newNode = new Node<T>(val, item);
item = newNode;
counter++;
}
void append(const int& val)
{
iterator newnode = new Node<T>(val);
newnode->next = NULL;
iterator last = item;
if (item == NULL)
{
item = newnode;
return;
}
while (last->next != NULL)
last = last->next;
last->next = newnode;
counter++;
}
int operator[](const int&);
private:
iterator item;
int counter;
};
让我们来看看你的begin
函数:
typedef Node<T>* iterator;
iterator begin() {
...
}
此函数返回一个Node<T>*
,一个指向Node<T>
对象的指针。结果,当你写
list.begin() + 2;
C++将其解释为"我有一个指针,我有一个数字,所以我会将指针向前迈入适当的步数。
然后你会问 - 好吧,等一下,为什么这个超载的操作员没有被调用?
template<class T>
Node<T>* operator+(const Node<T>& iter, const int& pos) {
...
}
看看参数类型。这个函数说"如果有人试图将一个诚实善良的Node<T>
对象和一个int
加在一起,这就是我希望你做的。问题是代码
list.begin() + 2
不会尝试在对象和整数中添加诚实到善良Node<T>
。相反,它会添加一个指向Node<T>
对象的指针和一个整数。由于这些类型与您的重载运算符不匹配,它甚至不会尝试调用重载运算符。
不幸的是,在C++中,您不能在两个基元类型之间重载运算符,因此无法编写一个接受Node<T>*
和int
的operator+
版本,因此这里的修复并不像"只需让您的operator+
函数接受Node<T>*
"那么简单。
相反,我建议将iterator
类型设置为实际的class
或struct
,而不是原始指针。您的迭代器可能会通过跟踪指向某处某个Node<T>
的指针来工作,但从根本上说,迭代器实际上不仅仅是该指针本身。例如,您可以尝试如下操作:
template <class T>
class List {
public:
class iterator {
public:
// some other things, and
iterator operator+ (int step) const;
private:
// some other things, and
Node<T>* current;
};
// some other things, and
iterator begin();
};
现在,您可以在List<T>::iterator
类型上重载operator+
。然后,operator+
的实现可以更新迭代器中存储的Node<T>*
。
希望这有帮助!
链表的iterator
不能是指针,它需要 像这样:
struct iterator
{
typedef int difference_type;
typedef T* pointer;
typedef T& reference;
typedef iterator_category std::bidirectional_iterator_tag
iterator();
iterator& operator++();
iterator& operator--();
iterator operator++(int);
iterator operator--(int);
T& operator*();
T* operator->();
bool operator==(iterator rhs) const;
bool operator!=(iterator rhs) const;
private:
iterator(Node*);
Node* node;
};
- 使用C++中的模板和运算符重载执行矩阵运算
- 为什么这个运算符<重载函数对 STL 算法不可见?
- <T> 通过模板化运算符重载将 std::complex 乘以双倍
- C++20概念:需要运算符重载
- 使用赋值运算符重载从类中返回jobject
- 在运算符重载定义中使用成员函数(const错误)
- 字节到位运算符重载C++
- 为什么在运算符重载时需要参考?
- 类中 c++ 的运算符 + 重载
- 算术复合运算符重载为非成员
- 运算符重载 (+),用于添加两个具有 C++ 的数组
- 交换运算符 + 重载会导致无限递归
- 如何理解新的运算符重载?
- 向量保持复数的运算符重载
- 如何创建运算符重载?
- 链接列表运算符重载没有打印出我想要的内容
- C++:需要帮助了解运算符重载错误
- 使用模板化运算符重载 XOR 运算符失败
- 如何确保接受的C++模板类型使运算符重载?
- 运算符重载使用运算符+添加类模板