I write a code for printing and finding sum of the nodes in the n-ary tree but I get an error and I cannot fix it.
#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);
}
error: 'children' was not declared in this scope q.push(children[i]); How can I fix it? What is the meaning of this error?
答案 0 :(得分:0)
Assuming you have included all the code here.
In main 'tree' is never created just used? And then children is out of scope because tree is never in any scope. Also if you have created another class to represent the tree and its in another file you need to include the header its defined in.
You haven't given a line number about the error or the steps you have already taken to resolve it.