简体   繁体   English

如何访问指向指向对象的指针列表的指针向量的向量数组中的前向列表?

[英]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.忽略 Person 或任何城市参数。 The error isn't due to the variables or classes.错误不是由变量或类引起的。 The error occurs when I write grid[i][j]->push_front(person);当我写 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?我认为 grid[i][j] 应该是一个指向前向列表的指针,所以如果我尊重它,那么我会回到前向列表吗? But it tells that it has no member "push_front".但它告诉它没有成员“push_front”。

Attention!注意力! The element in the grid is pointer.网格中的元素是指针。

As declared std::vector<std::vector<std::forward_list<Person*>*>*> grid;正如声明的std::vector<std::vector<std::forward_list<Person*>*>*> grid; , type of grid[i] is std::vector<std::forward_list<Person*>*>* . grid[i]类型是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*>* .所以grid[i][j]不会编译,而(*(grid[i]))[j]会编译,它的类型是std::forward_list<Person*>*

If want to perate to the forwardlist, the right code is (*(grid[i]))[j]->push_front(...) .如果想访问转发列表,正确的代码是(*(grid[i]))[j]->push_front(...)

Much more, declare the grid as type std::vector<std::vector<std::forward_list<Person*>>> is more easier way.更重要的是,将网格声明为std::vector<std::vector<std::forward_list<Person*>>>是更简单的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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