2

This has been asked before in Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream<char>&&’, the following code results in:

error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’

And the code:

// tried forward declaring but this did not help
namespace Tree {
    template<typename> class Node;
};
template<typename T> std::ostream& operator<<(std::ostream&, const Tree::Node<T>&);

namespace Tree {
    template<typename> class Tree;

    template<typename T>
    class Node {
        friend inline std::ostream& operator<<(std::ostream& out, const Node& node) {
            return out << node.getData();
        }
        friend class Tree<T>;
        // member declarations
        ...
    };

    template<typename T>
    class Tree {
        friend inline std::ostream& operator<<(std::ostream& out, const Tree& tree) {
            return tree.showInOrder(out, tree.root);
        }

        std::ostream& showInOrder(std::ostream& out, std::unique_ptr<Node<T>> const &node) const {
            if(node->left) showInOrder(out, node->left);
            out << node << " "; // error: cannot bind 'std:ostream {...} to lvalue'
            if(node->right) showInOrder(out, node->right);
            return out;
        }
    };
};

According to Bo Persson's answer in the above link, I have a problem with a "non-deducible context", but the accepted answer there does not resolve the problem.

Community
  • 1
  • 1

1 Answers1

5

node is a std::unique_ptr<Node<T>>. You need to dereference it:

out << *node << " ";
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324