-1

I would like to ask about a recursive function I am writing but I think I am not getting right the concepts of pointer, reference, and object itself.

This function reads a tree sequence from standard input and it should return the entire tree. First it comes the node value and second the number of child nodes. That is why I want it to be recursive: with afegir_fill func it joins a child to a node.

The tree class (Arbre) is already implemented (here HPP file) and the recursive function:

Arbre<int> read_arbre() {
    int arrel, fills;
    cin >> arrel;
    cin >> fills;
    Arbre<int> root(arrel);

    if (fills > 0) {
        for(int i = 0; i < fills; i++) {
            root.afegir_fill(read_arbre());
        }
    }

    return root;
}

The error I am getting is error: cannot bind non-const lvalue reference of type ‘Arbre<int>&’ to an rvalue of type ‘Arbre<int>’. So do I need to return a reference to the tree each time? How?

arbre.hpp

#include <iostream>
#include <cstddef>
using namespace std;

template <typename T>
class Arbre {
private:
  Arbre(): _arrel(NULL) {};
  struct node {
    T info;
    node* primf;
    node* seggerm;
  };
  node* _arrel;
  static node* copia_arbre(node* p);
  static void destrueix_arbre(node* p) throw(); 

public:
  // Constructs an Arbre with a x as unique node.
  Arbre(const T &x);

  // Rule of three.
  Arbre(const Arbre<T> &a);
  Arbre& operator=(const Arbre<T> &a);
  ~Arbre() throw();
  // b.afegir_fill(a) a tree becomes first child of b tree, and after that a becomes invalid.
  void afegir_fill(Arbre<T> &a);


  friend class iterador;
  class iterador {
  public:
    friend class Arbre;

    // Invalid iterator.
    iterador() throw();

    // Returns the sub-tree
    Arbre<T> arbre() const;

    // Returns the value.
    T operator*() const;

    // Returns first child.
    iterador primogenit() const;

    // Returns next brother.
    iterador seg_germa() const;

    // Operators.
    bool operator==(const iterador &it) const {
      return _p == it._p;
    };
    bool operator!=(const iterador &it) const {
      return _p != it._p;
    };
    static const int IteradorInvalid = 410;

  private:
    Arbre<T>::node* _p;
  };

  // Returns iterator to root.
  iterador arrel() const throw();

  // Returns invalid iterator.
  iterador final() const throw();

  static const int ArbreInvalid = 400;
};
Pere
  • 379
  • 2
  • 10
  • Make `Arbre root(arrel);` `static` so you can return a reference to it. Another option is to pass it through `read_arbre()` as a reference parameter. – πάντα ῥεῖ Oct 14 '18 at 18:12

1 Answers1

0

Finally solved, just stored the returned objected before calling again the function.

Arbre<int> read_arbre() {
    int arrel, fills;
    cin >> arrel;
    cin >> fills;
    Arbre<int> root(arrel);

    if (fills > 0) {
        for(int i = 0; i < fills; i++)
        {
            Arbre<int> fill = read_arbre();
            root.afegir_fill(fill);
        }
    }
    return root;
}
Pere
  • 379
  • 2
  • 10