C++ STL list functions segfaulting with empty list

前端 未结 1 1787
孤街浪徒
孤街浪徒 2021-01-16 11:06

I have a list of pointers as a member of a class. I instantiate that class, and various functions such as size() and empty() fail with a segfault when the list is empty. W

相关标签:
1条回答
  • 2021-01-16 11:37
    int Player::addToInv(Thing& t) {
    if (inventory.size() <= 52) {
        inventory.push_back(&t);
    } else {
        shiplog("Cannot add to inventory, 52 item limit reached",10);
        return 0;
    }
    

    }

    Thing is passed by reference, but then its address is passed to inventory.push_back(). Try just passing 't'.

    0 讨论(0)
提交回复
热议问题