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

时间:2015-01-29 06:27:18

标签: c++ vector

这是我的班级:

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 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;
        }
    }
}

当我尝试编译时,我在上面指出的位置出错。像'没有匹配功能来调用'的东西。我确定我在某处解除了错误。

1 个答案:

答案 0 :(得分:2)

notepadstd::vector<std::vector<item>>,但colstd::vector<item>::iterator,即它指的是item。 您无法从item中删除std::vector<std::vector<item>>

您要从其直接容器中删除colrow而不是notepad

row->erase(col);

您的命名有点令人困惑,因为它使得迭代器的当前项看起来像是“行”中的“列”。它不是,它是item