Оператор перегрузки <<: не может связать lvalue с ‘std :: basic_ostream <char> &&’

У меня есть класс, который использует вложенный класс, и хочу использовать вложенный классoperator<< определитьoperator<< в высшем классе. Вот как выглядит мой код:

#include <memory>
#include <iostream>

template<typename T>
struct classA {
  struct classB
  {
    template<typename U>
    friend inline std::ostream& operator<< (std::ostream &out,
                                            const typename classA<U>::classB &b);
  };

  classB root;

  template<typename U>
  friend std::ostream& operator<< (std::ostream &out,
                                   const classA<U> &tree);
};

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

template<typename T>
inline std::ostream& operator<< (std::ostream &out,
                                 const typename classA<T>::classB &b)
{
  return out;
}

int main()
{
  classA<int> a;
  std::cout << a;
}

При компиляции без поддержки C ++ 11, определение оператора << для внутреннего класса, похоже, не найдено компилятором:

so.hpp:24:7: error: no match for ‘operator<<’ in ‘out << tree.classA<int>::root’
so.hpp:24:7: note: candidates are: ...

С GCC 4.6 и 4.7 при компиляции с std = c ++ 0x:

so.hpp:21:3: error: cannot bind ‘std::ostream {aka std::basic_ostream<char>}’ lvalue to ‘std::basic_ostream<char>&&’
In file included from /usr/include/c++/4.7/iostream:40:0,
                 from so.hpp:2:
/usr/include/c++/4.7/ostream:600:5: error:   initializing argument 1 of ‘std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = classA<int>::classB]’

Может кто-нибудь сказать мне, почему этот код не является законным, и как лучше всего делать то, что я хочу?

Ответы на вопрос(3)

Ваш ответ на вопрос