2017-07-12 6 views
-5

印刷用のコードを作成し、n-aryツリー内のノードの合計を検索しますが、エラーが発生し修正できません。修正する方法このスコープでエラーが宣言されていません

#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); 
} 

エラー: '子供' がこの範囲 q.pushで宣言されていなかった(子供[I]); どうすれば修正できますか? このエラーの意味は?

+0

おそらく、あなたは 'node-> children'を意味しますか?そして意味は明らかです: 'print'関数に変数' children'はありません。 –

+0

'main'の' tree'も不明です。 – mch

答えて

0

すべてのコードをここに含めたとします。

メイン 'ツリー'では、使用されたことはありません。そして、木はどんなスコープにも決してないので、子供たちは範囲外です。また、ツリーとその別のファイルを表す別のクラスを作成した場合は、定義されているヘッダを含める必要があります。

解決済みの問題についての行番号や、それ。

関連する問題