-5

我编写了一个代码来打印和查找 n 叉树中节点的总和,但我得到一个错误并且我无法修复它。

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

class Node
{
public:
    Node(int input)
    {
        this->data = input;
    }

    vector<Node*> children;
    int data;
};

void print(Node *root)
{
    if (root == NULL)
        return;

    queue<Node *> q;

    q.push(root);
    int count = 0;

    while (q.size() != 0)
    {
        Node *node = q.front();
        cout << node->data << "  ";
        q.pop();

        int sum = 0;
        for (unsigned int i = 0; i < node->children.size(); i++)
        {
            q.push(children[i]);
            sum += (children[i]);
        }
        cout << " - ";
    }

    cout << "Sum: " << cout;
}

int main()
{
    tree->children.push_back(new Node(16));
    tree->children.push_back(new Node(96));
    tree->children.push_back(new Node(8));
    tree->children.push_back(new Node(10));
    tree->children.push_back(new Node(22));
    tree->children.push_back(new Node(9));
    tree->children.push_back(new Node(100));
    tree->children.push_back(new Node(1));
    tree->children.push_back(new Node(51));
    tree->children.push_back(new Node(70));

    cout << "Root: " << tree->data << endl << "Children:" << endl;
    for (unsigned int i = 0; i < 10; i++)
    {
        cout << tree->children[i]->data << " ";
    }
    cout << endl;

    print (tree);
}

错误:'children' 没有在这个范围内声明 q.push(children[i]); 我该如何解决?这个错误是什么意思?

4

1 回答 1

0

假设您已在此处包含所有代码。

在主“树”中从未创建只是使用?然后 children 超出范围,因为 tree 从不在任何范围内。此外,如果您创建了另一个类来表示树及其在另一个文件中,则需要包含其定义的标头。

您没有提供有关错误的行号或您已经采取的解决错误的步骤。

于 2017-07-12T08:13:41.693 回答