Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6702093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:00:44+00:00 2026-05-26T07:00:44+00:00

I’m getting the following error when trying to return the value using the operator*()

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T07:00:45+00:00Added an answer on May 26, 2026 at 7:00 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.