Reputation: 23
Here is my 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();
};
I created a class that has a vector of vectors of items as its private member:
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);
};
I am having trouble getting the removeItem()
function to compile. I want to allow the user to enter the item the would like to remove from a list by having them enter a string that matches the list name (first item's name in the vector is list name):
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;
}
}
}
When I try to compile I get an error at the position I indicated above. Something like 'no matching function for call to'. I'm sure I'm dereferencing something wrong somewhere.
Upvotes: 0
Views: 198
Reputation: 66371
notepad
is a std::vector<std::vector<item>>
, but col
is a std::vector<item>::iterator
, i.e. it refers to an item
.
You can't remove an item
from a std::vector<std::vector<item>>
.
You want to remove col
from its immediate container, which is row
, not notepad
:
row->erase(col);
Your naming is slightly confusing, as it makes it seem like the iterator's current item is a "column" inside a "row". It isn't, it's an item
.
Upvotes: 2