-1

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.

4
  • Did you miss std:: before find? Commented Feb 6, 2023 at 7:36
  • 1
    where is equal operator for Items? Commented Feb 6, 2023 at 7:37
  • @kiner_shah Argument Dependent Lookup. Sadly iterator of std::vector do not have to be in std: namespace. Commented Feb 6, 2023 at 7:38
  • Need to see full errors for better analysis. Commented Feb 6, 2023 at 7:39

1 Answer 1

0

std::find uses operator== to compare elements so you need to add an operator== overload for comparing two Items:

struct Items 
{
    bool in_use = false;
    int item_no;
    std::string item_name;

    // Example:
    bool operator==(const Items& o) const {
        return in_use == o.in_use
            && item_no == o.item_no
            && item_name == o.item_name;
    }
};

or simply

struct Items 
{
    bool in_use = false;
    int item_no;
    std::string item_name;

    bool operator==(const Items& o) const = default;

    // C++20 version:
    //auto operator<=>(const Items& o) const = default;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Well that worked. Thank you kindly. I hadn't read anything mentioning the operator overload needed, so I got lost in the errors regarding == operations. Thank you again.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.