Error finding data for vector children [Trees]

Okay, so I'm currently learning trees in class right now and decided to self-experiment with some code to kind of understand how trees work and are organized, but I've run into a problem that I do not understand...

So the basic idea is that I'm just making nodes that act as people who have a name, their health (like 92, or 96), and their overall score (like 82, 90, 100), and potential children. Don't mind the variable choices...

I wanted to make a function where the user inputs the data for the person, then if the person has children, recurse through the same function and so on until the choice is not y/Y and the function ends.

By using the vector, I can create dynamic space for however many children the person has (as inputted by user) and can repeat the process using a for loop for every child of the parent, until it reaches a nullptr (if t != nullptr).

By resetting the childrenCount to 0 every time the function recurses, I will be able to create x many new children if the (now main) person has children.

If any of what I 'understand' about trees or vectors is incorrect above, please do correct them >_<, I just also recently learned about vectors and have yet to really implement them in my own functions so I feel a bit lost.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <vector>
#include <iostream>

using namespace std;

struct Node
{
    int health;
    int overall;
    string name;
    vector<Node*> children; //Vector for storing children
};

//Function Prototype(s)
void findData(Node* &t);

//int main
int main ()
{
    Node* humans = new struct Node; //Tree
    findData(humans);
    
    //INCOMPLETE
    return 0;
}

void findData(Node* &t)
{
    int childrenCount = 0;
    char enter;
    cout << "Enter the person's name: ";
    getline(cin, t->name); //This is where the error occurs
    cout << "Enter " << t->name << "'s health: ";
    cin >> t->health;
    cout << "Enter " << t->name << "'s overall #: ";
    cin >> t->overall;
    cout << "Does this person have any children? ";
    cin >> enter;
    if (enter == 'Y' || enter == 'y')
    {
        cout << "How many children? ";
        cin >> childrenCount;
        t->children.resize(childrenCount);
        
        for (int i = 0; i < t->children.size(); i++)
            findData(t->children[i]);
    }
    else
        cout << "Okay. Finished finding data." << endl;
}


The error I receive is at the line where I'm re-entering the data for the children[0]'s name after recursing once. It takes me to this separate place in xcode (which I assume I have yet to learn about) and says the following:

1
2
3
4
_LIBCPP_INLINE_VISIBILITY
bool __is_long() const _NOEXCEPT
   {return bool(__r_.first().__s.__size_ & __short_mask);}
//Thread 1: EXC_BAD_ACCESS (code=1, address=0x8) 


Potential problems/ideas I thought might have been the case include
1. Maybe it has to do with the parameter I've chosen?
2. Yeah that's it... I'm clueless

Thanks
Last edited on
You need to create the children before trying to use them.
I resized the vector with childrenCount and tried debugging and it does say that t has children[0] through children[whatever I enter], but they are all set to NULL.

I tried push_back(t) just to create space for the vector but it still does not work. This following thing was inserted as a replacement to line 43 but the result is the same:
1
2
for (int i = 0; i < childrenCount; i++)
            t->children.push_back(t); //or maybe nullptr? 


How would you 'create' the children before trying to use them?
The vector contains pointers so when you resize the vector you get a bunch of null pointers. You need to actually create the Node objects, like you do in main with the root node.

Is there a reason why children is a vector of Node pointers and not a vector of Nodes (vector<Node> children;)? That way the Node objects would get created automatically when you resize the vector, and you wouldn't need to delete them explicitly.
Last edited on
Understood. It works once I switch children to <Node> and the t->children to t.children

Small question though - would it be possible to implement a similar function using Node*, or is it too advanced or not really applicable?

Thanks for the advice.
Yes, all you need to do is to create all children nodes using new.

1
2
for (int i = 0; i < childrenCount; i++)
            t->children.push_back(new Node);


Note that when you create something with new you are responsible for deleting it when you no longer need it, otherwise the memory will be in use until the program ends.

http://www.cplusplus.com/doc/tutorial/dynamic/#delete
Topic archived. No new replies allowed.