Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

find() causing "template argument" compilation errors when using struct as input. Any suggestions?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

#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.

>Solution :

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;
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading