简体   繁体   中英

Array getting passed incorrectly to a function in C++

I'm working a project for class that requires creating a binary search tree of criminal names with up to 8 attributes per criminal.

I set up a string array att[] that will read in the attributes for each criminal, and then be passed to my BSTInsert class function. Through debugging I can see that the array is correct when it's just in the setupTree function. Once it's passed to BSTInsert, instead of having each string it only has one string, and on top of that nothing is copied from the array to the node in the tree.

Can anyone tell me what I'm doing wrong?

Here's my code for setting up the tree:

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

Here's my class function for inserting a node:

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;

treetype att[] doesn't pass an array, it passes a pointer to an array - it decays to treetype att* .

That said, your problem is here:

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

This copies the wrong element of att (beyond the end of the array) into every att in newNode. What you meant was

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM