2016-04-29 24 views
2

私は、犯罪者ごとに最大8つの属性を持つ犯罪名のバイナリ検索ツリーを作成する必要があるクラスのプロジェクトを行っています。配列がC++の関数に正しく渡されない

私は、各犯罪者の属性を読み取る文字列配列att []を設定してから、私のBSTInsertクラス関数に渡します。デバッグを行うと、配列がsetupTree関数内にあるときに正しいことがわかります。 BSTInsertに渡されると、各文字列を持つ代わりに文字列が1つしかなく、その上に配列からツリーのノードに何もコピーされません。

誰も私が間違ってやっているものを私に伝えることができますか?ここで

は、ツリーを設定するための私のコードです:

void setupTree(BST& criminals) 
{ 
    ifstream fin("criminals.txt"); 
    string temp; 

    fin >> temp; 
    //FINISHED means it has all the criminals 
    while (temp != "FINISHED") 
    { 
     //SUSPECT lets it know to read in a new name and new attributes 
     if (temp == "SUSPECT") 
     { 
      string name; 
      string att[8]; 
      int count = 0; 
      fin >> temp; 

      //if there is a false "suspect" line, quit 
      if (temp == "FINISHED") return; 
      name = temp; 
      fin >> temp; 

      while (temp != "SUSPECT" && temp != "FINISHED") 
      { 
       att[count] = temp; 
       count++; 
       fin >> temp; 
      } 

      criminals.BSTInsert(name, att, count); 
     } 
    } 
} 

ここで私のクラスの機能は、ノードを挿入するためです:

bool BST::BSTInsert(treetype name, treetype att[], int count) 
{ 
//gets the memory for the node. If unable, returns fail. 
node* newNode = new node; 
if (newNode == NULL) 
{ 
    return false; 
} 

newNode->count = 0; 

//initializes the node with the given information to place 
for (int i = 0; i < count; i++) 
{ 
    newNode->att[newNode->count] = att[count]; 
    newNode->count++; 
} 
newNode->name = name; 
newNode->left = newNode->right = NULL; 

//if the tree is empty, creates this node as the root 
if (root == NULL) 
{ 
    root = newNode; 
    root->parent = NULL; 
} 
else 
{ 
    //the tree is not empty, so it will use the parent to insert the node 
    node* current = root; 
    node* parent = NULL; 

    //finds the insertion spot 
    while (current != NULL) 
    { 
     parent = current; 
     if (name <= current->name) 
     { 
      current = current->left; 
     } 
     else 
     { 
      current = current->right; 
     } 
    } 
    //inserts the new node onto the correct side of the parent 
    if (name <= parent->name) 
    { 
     parent->left = newNode; 
    } 
    else 
    { 
     parent->right = newNode; 
    } 
    newNode->parent = parent; 
} 
return true; 
+2

'while(temp!=" SUSPECT "temp!=" FINISHED ")'ですか? – drescherjm

+0

配列の長さが分からないため、デバッガには1つの項目しか表示されないことは確かですか?デバッガでatt [1]を表示しようとするとどうなりますか? –

+0

デバッガの使い方を知っています。すばらしいです!さて、あなたのデバッガは、BSTInsert()のforループの中で何が起こるのでしょうか?あなたは、新しいノードの 'att'パラメータから' att'メンバに属性をコピーしようとするループは知っていますか?なぜこのforループを踏んだりして、属性が正しくコピーされているかを見てみませんか?か否か。 –

答えて

1

treetype att[]は、配列を渡していない、それはANにポインタを渡します配列 - それはtreetype att*に崩壊します。

あなたの問題はここにある、言った:

for (int i = 0; i < count; i++) 
{ 
    newNode->att[newNode->count] = att[count]; 
    newNode->count++; 
} 

このコピーは、(配列の終わりを越えて)ATTの間違った要素newNode内のすべてのattには。あなたが意味していたのは、

for (int i = 0; i < count; i++) 
{ 
    newNode->att[newNode->count] = att[newNode->count]; 
    newNode->count++; 
} 
+0

は「平均でない」と同じ意味ですか? :D –

関連する問題