【问题标题】:How to fix was not declared in this scope error在此范围错误中未声明如何修复
【发布时间】:2017-07-12 07:58:53
【问题描述】:

我编写了一个代码来打印和查找 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(儿童[i]); 我该如何解决? 这个错误是什么意思?

【问题讨论】:

  • 也许你的意思是node-&gt;children?而且意思很明确:print函数中没有变量children
  • tree in main 也是未知的。

标签: c++ pointers tree n-ary-tree


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多