проблема с неполным типом и forward declaration

222
04 апреля 2018, 10:43

есть класс бинарного дерева:

class BinaryTree
{
    friend class bt_iterator_depth;
    struct Elem
    {
        int value;
        Elem *left;
        Elem *right;
        Elem(int v, Elem *l = nullptr, Elem *r = nullptr)
            :value(v), left(l), right(r){}
    };
    Elem *root;
public:
    ...
    bt_iterator_depth begin_d()
    {
        return bt_iterator_depth(root);
    }
    bt_iterator_depth end_d()
    {
        return bt_iterator_depth();
    }
};

и класс итератора для обхода этого дерева в глубину:

class bt_iterator_depth
{
    BinaryTree::Elem *node;
    std::stack<BinaryTree::Elem *> st
public:
    ...
    bt_iterator_depth operator++()
    {
        if(!st.empty())
        {
            node = st.top();
            if(node->left)
                st.push(node->left);
            if(node->right)
                st.push(node->right);
        }
        else
            node = nullptr;
        return *this;
    }
    bt_iterator_depth operator++(int)
    {
        if(!st.empty())
        {
            node = st.top();
            if(node->left)
                st.push(node->left);
            if(node->right)
                st.push(node->right);
        }
        else
            node = nullptr;
        return *this;
    }
};

Компилятор пишет:

BinaryTree.hpp:140:3: error: 'bt_iterator_depth' does not name a type
   bt_iterator_depth begin_d()
   ^~~~~~~~~~~~~~~~~
BinaryTree.hpp:144:3: error: 'bt_iterator_depth' does not name a type
   bt_iterator_depth end_d()
   ^~~~~~~~~~~~~~~~~

Если добавить class bt_iterator_depth перед определением класса BinaryTree, то появляются ошибки такого типа:

BinaryTree.hpp: In member function 'BT::bt_iterator_depth                     
BT::BinaryTree::begin_d()':
BinaryTree.hpp:141:3: error: return type 'class BT::bt_iterator_depth' is 
incomplete
   {
   ^
BinaryTree.hpp:142:33: error: invalid use of incomplete type 'class BT::bt_iterator_depth'
   return bt_iterator_depth(root);
                           ^
BinaryTree.hpp:8:8: note: forward declaration of 'class BT::bt_iterator_depth'
class bt_iterator_depth;
    ^~~~~~~~~~~~~~~~~

подскажите, пожалуйста, как поправить?

READ ALSO
Как вывести слова встречающиеся в обоих файлах?

Как вывести слова встречающиеся в обоих файлах?

Имеются 2 файла, в каждом из них словаНужно вывести на экран консоли слова, которые встречаются в обоих файлах

216
Взятие структуры из класса

Взятие структуры из класса

в сlass Points имеется структура

207
GTest и TearDown

GTest и TearDown

Когда происходит FAIL() тогда TearDown не освобождает ресурсы, можно ли как то сделать чтоб когда - происходит FAIL() происходил вызов деструктора...

238