从向量中的向量中删除 class 的实例

Remove instance of a class from a vector within a vector

这是我的 class:

class item
{
private:
    std::string name;
    double price;
    int quantity;

public:
    item();
    void setName(std::string itemName);
    std::string getName();
    void setPrice(double itemPrice);
    double getPrice();
    void setQuantity(int itemQuantity);
    int getQuantity();
};

我创建了一个 class,它有一个项目向量的向量作为它的私有成员:

class list
{
private:
    std::vector<std::vector<item>> notepad;

public:
    bool isEmpty();
    void addList();
    void printLists(bool printTotalPrice);
    void addItem();
    void removeItem();
    void editItem();
    void importList(std::ifstream& iFile);
    void exportList(std::ofstream& oFile);
};

我无法编译 removeItem() 函数。我想让用户输入要从列表中删除的项目,方法是让他们输入与列表名称匹配的字符串(向量中第一个项目的名称是列表名称):

void list::removeItem()
{
    if (isEmpty() == true)
    {
        std::cout << "You have not added any lists yet." << std::endl;
    }
    else
    {
        bool toPrint = false;
        bool matchFound = false;
        std::string userListChoice;

        printLists(toPrint);
        std::cout << "Which list would you like to add to?" << std::endl;
        std::cout << "Please enter the exact list name." << std::endl;
        std::cin >> userListChoice;

        for (unsigned int i = 0; i < notepad.size(); i++)
        {
            if (userListChoice == notepad[i][0].getName())
            {
                matchFound = true;
                bool itemMatchFound = false;
                std::string userItemInquiry;

                std::cout << "Current List Items:" << std::endl;
                for (unsigned int j = 1; j < notepad[i].size(); j++)
                {
                    std::cout << notepad[i][j].getName() << std::endl;
                }
                std::cout << "Which item would you like to remove?" << std::endl;
                std::cout << "Please enter the exact item name." << std::endl;
                std::cin >> userItemInquiry;

                std::vector<std::vector<item>>::iterator row;
                std::vector<item>::iterator col;

                for (row = notepad.begin(); row != notepad.end(); ++row)
                {
                    for (col = row->begin() + 1; col != row->end(); ++col)
                    {
                        if (col->getName() == userItemInquiry)
                        {
                            itemMatchFound = true;
    PROBLEM HERE ---->      notepad.erase(col);
                            std::cout << "Item has been removed." << std::endl;
                            break;
                        }
                        else
                        {
                            itemMatchFound = false;
                        }
                    }
                }
                if (itemMatchFound == false)
                {
                    std::cout << "The item name you entered was not found." << std::endl;
                    std::cout << "Please make sure you enter the exact name of " << std::endl;
                    std::cout << "the item, accounting for spaces and capitalization." << std::endl;
                }
            }
        }

        if (matchFound == false)
        {
            std::cout << "The list name you entered was not found." << std::endl;
            std::cout << "Please make sure you enter the exact name of the" << std::endl;
            std::cout << "list, accounting for spaces and capitalization." << std::endl;
        }
    }
}

当我尝试编译时,在我上面指出的位置出现错误。类似于 'no matching function for call to'。我确定我在某处取消引用了错误的内容。

notepadstd::vector<std::vector<item>>,但 colstd::vector<item>::iterator,即它指的是 item.
您不能从 std::vector<std::vector<item>> 中删除 item

您想从其直接容器 row 中删除 col,而不是 notepad:

row->erase(col);

您的命名有点令人困惑,因为它使迭代器的当前项看起来像是 "row" 中的 "column"。不是,是 item.