简体   繁体   中英

How to access forward lists in a vector array of pointers to vectors of pointers to forward lists of pointers to objects?

Here's the relevant piece of code:

std::vector<std::vector<std::forward_list<Person*>*>*> grid;

for (int i = 0; i < criticalValuex; i++) {
    std::vector<std::forward_list<Person*>*>* list;
    for (int j = 0; j < criticalValuey; j++) {
        std::forward_list<Person*>* flist;
        list->push_back(flist);
    }
    grid.push_back(list);
}
for (Person *person : people) {
    int i = (int)((person -> getPosition().x - city.getPosition().x + citySize.x / 2) / cellSize);
    int j = (int)((person -> getPosition().y - city.getPosition().y + citySize.y / 2) / cellSize);
    if (i < citySize.x / cellSize && j < citySize.y / cellSize) {
        grid[i][j]->push_front(person);
    }
}

Ignore the Person or any city parameters. The error isn't due to the variables or classes. The error occurs when I write grid[i][j]->push_front(person); even though it should work.

I think that grid[i][j] should be a pointer to a forward list so if I deference it, then I get back the forward list right? But it tells that it has no member "push_front".

Attention! The element in the grid is pointer.

As declared std::vector<std::vector<std::forward_list<Person*>*>*> grid; , type of grid[i] is std::vector<std::forward_list<Person*>*>* . It is not an array but a pointer of array. So grid[i][j] wont compile, while (*(grid[i]))[j] does, and its type is std::forward_list<Person*>* .

If want to perate to the forwardlist, the right code is (*(grid[i]))[j]->push_front(...) .

Much more, declare the grid as type std::vector<std::vector<std::forward_list<Person*>>> is more easier way.

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