简体   繁体   English

访问 shared_ptr 矢量对象的问题

[英]Problem with accessing shared_ptr vector objects

I have a problem with accessing shared_ptr Vector from main.我从 main 访问 shared_ptr Vector 时遇到问题。 My turnOrder function takes 2 sharedPtr vectors, combines and sorts them and puts the objects to another vector( Units ).The problem is, when I test the function using for loops it gives exactly what I want but in the int main(), Units vector seems empty and when I try to access any object it gives this exit code: " exit code -1073741819 (0xC0000005) ".我的turnOrder function 采用 2 个 sharedPtr 向量,将它们组合和排序并将对象放入另一个向量( Units )。问题是,当我使用 for 循环测试 function 时,它给出了我想要的,但在 int main(), Units矢量似乎是空的,当我尝试访问任何 object 时,它会给出这个退出代码:“退出代码 -1073741819 (0xC0000005)”。

void turnOrder( std::vector<std::shared_ptr<Monster>> monsters, std::vector<std::shared_ptr<Hero>> heroes,  std::vector<std::shared_ptr<Unit>> Units) {

    std::vector<std::shared_ptr<Unit>> units;

    units.reserve(8);

    units.insert(units.begin(), heroes.begin(), heroes.end());
    units.insert(units.end(), monsters.begin(), monsters.end());
//TESTING INITIAL VECTOR

    for(int i = 0; i < units.size(); i++){
        units[i]->printOut();
    }
    for (int i = 0; i < units.size(); i++) {
        units[i]->findSpeedRate();
    }
    struct X{

        inline bool operator() ( const std::shared_ptr<Unit> obj1, const std::shared_ptr<Unit> obj2){
            return(obj1->speedRate > obj2->speedRate);
        }
    };
    std::sort(units.begin(), units.end(), X());

    for(int i = 0; i < units.size(); i++){
        Units.emplace_back(units[i]);
    }

//TESTING ORDERED VECTOR
    for(int i = 0; i < Units.size(); i++){
        Units[i]->printOut();
    }

}


int main(){

    std::vector<std::shared_ptr<Unit>> Units;
    std::vector<std::shared_ptr<Monster>> monsters;
    std::vector<std::shared_ptr<Hero>> heroes;

    auto crusader1 = std::make_shared<Crusader>(1);
    heroes.emplace_back(crusader1);
//It goes the same with the other objects(monsters and heroes)

    turnOrder(monsters, heroes, Units);
    Units[0]->printOut();


}

Pass vectors by reference, not by value.通过引用而不是值传递向量。 You are modifying copies of vectors inside the turnOrder .您正在修改turnOrder内的向量副本 Simply change the declaration to只需将声明更改为

void turnOrder( std::vector<std::shared_ptr<Monster>>& monsters, std::vector<std::shared_ptr<Hero>>& heroes,  std::vector<std::shared_ptr<Unit>>& Units) {...}

The difference is & for each variable.每个变量的区别是& Also, please refer for this question for additional info.另外,请参阅此问题以获取更多信息。

The other poster has a point about copies but I would take a look at this and say if you are going to populate units in this function why not just return units and remove the units parameter.另一张海报有一个关于副本的观点,但我会看看这个并说如果你要在这个 function 中填充单位,为什么不只返回单位并删除单位参数。 Then然后

Units=turnOrder(monsters, heroes)

makes lots of sense.很有意义。

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

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