Darestium
Darestium

Reputation: 653

Pointers, Vector Lists = Mass Confusion

If you could suggest a more appropriate title I would be forever in your debt.

The issue: I have a function which is recursive in nature, it aims to get each child from each quad and return it to the original caller. The problem is that I get the following every time I run the program (in my console window):

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

I aim to retrieve each child as a pointer.

Here's the caller:

std::vector<Quad*> children = root->get_children_recursive(player_pos);

And here's the function itself:

std::vector<Quad*> Quad::get_children_recursive(glm::vec3 player_pos)  {
    std::vector<Quad*> out_children;

    if (children.size() != 0)  {
        for (int i = 0; i < children.size(); i++)  {
            Quad *child = &children[i];

            std::vector<Quad*> childs_children = child->get_children_recursive(player_pos);

            for (int j = 0; j < childs_children.size() - 1; j++)  {
                Quad *childs_child = childs_children.at(j);

                out_children.emplace_back(childs_child);
            }
        }
    }

    if (this->should_draw(player_pos))  {
        out_children.emplace_back(this);
    }

    return out_children;
}

If you would like me to supply more code or any other details I would be more than happy to oblige.

(every time I attempt to use the debugger and the program gets to the line that causes the aforementioned error I get a BSOD :O)

Edit:

Header:

class Quad  {
    public:
...
        std::vector<Quad*> get_children_recursive(glm::vec3 player_pos);
...
    protected:
    private:
...

        std::vector<Quad> children;
...
};

Upvotes: 0

Views: 200

Answers (2)

Darestium
Darestium

Reputation: 653

for (int j = 0; j < childs_children.size() - 1; j++)  {

to

for (int j = 0; j < childs_children.size(); j++)  {

Upvotes: 0

Lyubomir Vasilev
Lyubomir Vasilev

Reputation: 3030

Is children a vector<Quad> or vector<Quad*>? If it is vector<Quad*>, then this line is wrong:

Quad *child = &children[i];

because it will take the address of the pointer. Then

child->get_children_recursive(player_pos)

would most probably lead to the error you are getting.

To fix that, just remove the &.

Upvotes: 1

Related Questions