我很难尝试重载*运算符.我试图使用它来取消引用指针.我已经发布了我正在尝试使用的内容.现在,当我尝试使用它时,我得到以下错误间接需要指针操作数(‘Iterator’无效)
//用法
Iterator List::Search(int key) { Iterator temp(head); while (!temp.isNull()) { if (*temp == key) { //return temp; cout << *temp << endl; } temp++; } return NULL; }
//头文件
class Iterator { public: Iterator &operator *(const Iterator &) const; private: node* pntr; };
// CPP文件
Iterator &Iterator::operator *(const Iterator & temp) const { return temp.pntr; }
解决方法
一元反复数运算符不需要参数.它也不太可能返回Iterator的引用.在这种情况下,我希望它返回对节点的引用.请注意,允许通过const运算符对数据进行可变访问,并提供仅允许const访问的ConstIterator类型是惯用的:
class Iterator { public: node& operator*() const; node* operator->() const; private: node* pntr; }; node& Iterator::operator*() const { return *pntr; } node* Iterator::operator->() const { return pntr; } node& Iterator::operator*() { return *pntr; }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。