I'm trying to code a set of functions for a game's inventory, but the function to remove an item from the inventory has become a road block. Essentially all it needs to do is find the record of a particular item in the vector, and remove it. Running the code below produces about 60 lines of errors with this being one of the few things I can interpret:
see reference to function template instantiation '_InIt std::findstd::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>,Items>(_InIt,const _InIt,const _Ty &)'
My fluency in compiler-ese is not good enough to make sense of much else. Anyway here is the code I'm using to test the function:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Items
{
bool in_use = false;
int item_no;
std::string item_name;
};
std::vector<Items> inventory;
void remove_items(Items);
int main()
{
Items item1 = {false, 1, "Shovel"};
Items item2 = {true, 2, "Lantern"};
Items item3 = {false, 3, "Book"};
inventory.push_back(item1); inventory.push_back(item2); inventory.push_back(item3);
remove_items(item2);
return 0;
}
void remove_items(Items i)
{
// COMPILER ERRORS SEEM TO PIN-POINT THIS LINE BELOW AS THE PROBLEM.
std::vector<Items>::iterator iter = find(inventory.begin(), inventory.end(), i);
inventory.erase(iter);
}
I've gone through a number of forum posts and articles on using structs in vectors with the find() function being used in a similar context, but I'm still not understanding the problem. My only guess is that the struct type is causing a problem. I've tried this same code without a struct, and filling the vector with integer variable entries, it compiled and ran without error, so I know this works with simpler data types. I've also tried a struct with just one integer type member, the same errors occurred, so I don't think the types within the struct are a problem. Any suggestions here, I'm completely lost on this one. The compilation errors just continue pointing to find() as problematic. I'm compiling from the Developer Command Prompt for Vs 2022.
First time poster, so any suggestions on formatting here would be welcome.