G
G
Grigory Dikiy2016-04-07 08:11:55
C++ / C#
Grigory Dikiy, 2016-04-07 08:11:55

Passing tree node?

Good day, I ran into the problem of passing tree nodes to functions. The task is simple, I need to pass the nodes to the function, and the function itself will work with this node further. But I get a compile-time error:
196|error: request for member 'next' in '* root', which is of pointer type 'Node*' (maybe you meant to use '->' ?)|

// Код узла дерева
class Node
{
    public:
        Token * value;       // Данные
        vector<Node *> next; // Список указателй на другие узлы

        // Деструктор
        ~Node()
        {
            delete value;

            int it = 0;
            for(; it < next.size(); it++)
                if(next[it] != nullptr)
                    delete next[it];
        }

        Node()
        {
            value = new Token();
        }

        void reverse(vector<Token *> &res)
        {
            int it = 0;
            for(; it < next.size(); it++)
                if(next[it] != NULL)
                    next[it]->reverse(res);

            res.push_back(value);
        }

        void Log()
        {
            cout << value->value << " ";
            int it = 0;
            for(; it < next.size(); it++)
                if(next[it] != NULL)
                    next[it]->Log();
        }
}

// Передача узла и вызов
bool Syntax::On_BG(Node ** root)
{
    if(++it >= lexList.size()) return false;
    if(lexList[it]->type != BEGIN) return false;

    Node * node;

    if(!On_BF(&node)) return false;
    *root->next.push_back(node);

    if(++it >= lexList.size()) return false;
    if(lexList[it]->type != END) return false;

    return true;
}

A little explanation: On_BF(&node) function, if everything goes well, assigns the address of the generated node to the pointer, then I try to push this address into the parent node, but an error occurs.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-04-07
@frilix

The dereference operation has a lower priority than the operation of selecting an element by a pointer, i.e. in *root->next.push_back(node); the order of operations is *(root->)next.push_back(node);, which, apparently, is not what you wanted, arrange the brackets like this: (*root)->next.push_back(node); and there will be happiness.
In general, what's the point in passing a pointer to a pointer in On_BG? Judging by the code, a simple pointer is enough, or even better - links.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question