C++ b 树的自定义迭代器中的运算符 * 错误

发布于 2024-12-10 14:07:45 字数 1482 浏览 1 评论 0原文

当我尝试在自定义迭代器中使用运算符 *() 返回值时,出现以下错误。

 error: invalid initialization of non-const reference of type 'char&' from a temporary of type 'char'

如果有人能够帮助我,那就太好了!

代码具体如下:

测试代码:

  for(btree<char>::iterator itr = tree.begin(); itr != tree.end(); ++itr) {

    cout << *itr << " "; // <- this line complains about the * operator.
  }

operator*() 代码:

template <typename T> typename btree_iterator<T>::reference btree_iterator<T>::operator*() const {

  return pointee_->value(); // <- this line is where the error above is produced from.
}

btree_iterator 类中的 pointee_ 是一个私有变量,定义如下:

btree_node<T>* pointee_;

reference 在 btree_iterator 类中被 typefef'ed 如下:

  typedef T&       reference;

定义了 value()在 btree_node 类中,如下所示:

T value() const;

// function definition.
template <typename T> T btree_node<T>::value() const { return value_; }

值最初通过其构造函数存储到 btree_node 中,如下所示:

template <typename T> btree_node<T>::btree_node(const T& elem) : value_(elem), nextCont_(NULL), prevCont_(NULL), nextNode_(NULL), prevNode_(NULL) {}

如果我遗漏了任何重要信息,请告诉我,如果遗漏了,请告诉我!

根据错误,听起来我正在尝试返回对局部变量的引用。迭代器本身可能是本地的,但它返回的值不应该是本地的,因为节点构造函数最初接受引用,并将其存储到节点中。

预先感谢并致以问候!

I'm getting the following error when trying to return the value using the operator*() in my custom iterator.

 error: invalid initialization of non-const reference of type 'char&' from a temporary of type 'char'

If anyone is able to help me out, that would be great!

The specifics of the code is as follows:

Test code:

  for(btree<char>::iterator itr = tree.begin(); itr != tree.end(); ++itr) {

    cout << *itr << " "; // <- this line complains about the * operator.
  }

The operator*() code:

template <typename T> typename btree_iterator<T>::reference btree_iterator<T>::operator*() const {

  return pointee_->value(); // <- this line is where the error above is produced from.
}

pointee_ from the btree_iterator class is a private variable defined as follows:

btree_node<T>* pointee_;

reference is typefef'ed as the following in the btree_iterator class:

  typedef T&       reference;

value() is defined in the btree_node class as follows:

T value() const;

// function definition.
template <typename T> T btree_node<T>::value() const { return value_; }

value is initially stored into a btree_node via it's constructor as follows:

template <typename T> btree_node<T>::btree_node(const T& elem) : value_(elem), nextCont_(NULL), prevCont_(NULL), nextNode_(NULL), prevNode_(NULL) {}

Please let me know if i missed including any crucial information, and apologies if i did!

Based on the error, it sounds like I'm trying to return a reference to a local variable. The iterator itself may be local, but the value that it is returning shouldn't be, since the node constructor originally takes in a reference, and stores that into the node.

Thanks in advance, and regards!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-12-17 14:07:45

您正在尝试使用对临时变量的引用

template <typename T> T btree_node<T>::value() const { return value_; }
                     ^^^

这将返回 value_ 的临时副本。

template <typename T> typename btree_iterator<T>::reference btree_iterator<T>::operator*() const {
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  return pointee_->value(); // <- this line is where the error above is produced from.
}

这将返回对该临时副本的引用。您很可能希望 value() 返回一个引用,但这取决于您 - 引用允许您的 btree 的客户端更改内容。特别是如果树是根据节点的值排序的,您可能不希望允许这样做。思考一下这与您的设计有何相关性。

You're trying to use a reference to a temporary variable

template <typename T> T btree_node<T>::value() const { return value_; }
                     ^^^

This returns a temporary copy of value_.

template <typename T> typename btree_iterator<T>::reference btree_iterator<T>::operator*() const {
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  return pointee_->value(); // <- this line is where the error above is produced from.
}

This returns a reference to that temporary copy. You most likely want value() to return a reference, but this is up to you - a reference allows clients of your btree to alter the contents. Especially if the tree is ordered based upon the value of the node, you may not want to allow this. Have a think how this is relevant to your design.

暖树树初阳… 2024-12-17 14:07:45

pointee_->value() 返回一个基本上是新副本的值。因此,该值在 operator* 中是本地的,并且您无法返回对此的引用。

我建议像大多数容器一样进行操作,即同时拥有 iterator 和 const_iterator,而 const_iterator 返回 const 引用,而 iterator 返回引用。同样的原则也适用于 value()。有返回 ref 和 const ref 的版本。

如果您想要处理副本,您可以根据客户端代码中的引用创建它们。如果您不想允许更改值,只需删除(或设为私有,或其他)非常量部分即可。它认为它也符合使用 btree 的人们所期望的:

迭代器不会在某个地方进行复制,而是对数据结构内的数据进行迭代。如果您想要副本,您可以制作它们。

pointee_->value() returns a values which is basically a fresh copy. Hence this value is local in operator* and you can't return a reference to that.

I'd recommend doing it just like most containers do, i.e. having both, iterator and const_iterator, whereas const_iterator returns a const reference and iterator returns a reference. The same principle applies to value(). Have versions returning a ref and a const ref.

If you ever want to work on copies, you can create them from the reference in client code. If you don't want to allow chanign the values, simply drop (or make private, or whatever) the non-const parts. It think it also fits what people using your btree expect:

Iterators do not make copies somewhere but you iterator over the data inside teh data structure. If you want copies, you can make them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文