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

huangapple go评论59阅读模式
英文:

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

问题

以下是翻译好的代码部分:

我正在尝试编写一组用于游戏库存的函数,但从库存中删除物品的函数已经成为一个障碍。它基本上只需要在向量中找到特定物品的记录,然后将其删除。运行下面的代码会产生大约60行错误,其中这是我能解释的少数几个之一:

看到函数模板实例化的引用 'std::find<_InIt std::vector<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,Items>(_InIt,const _InIt,const _Ty &)'  

我的编译语言流利度不够,不能理解太多。无论如何,这是我用来测试该函数的代码:

#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)
{
    // 编译错误似乎将此行定位为问题所在。
    std::vector<Items>::iterator iter = find(inventory.begin(), inventory.end(), i);

    inventory.erase(iter);
}

请注意,我仅提供了代码部分的翻译,没有其他内容。

英文:

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::find<std::_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 &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;algorithm&gt;
#include &lt;string&gt;

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

std::vector&lt;Items&gt; inventory;

void remove_items(Items);

int main()
{
    Items item1 = {false, 1, &quot;Shovel&quot;};
    Items item2 = {true, 2, &quot;Lantern&quot;};
    Items item3 = {false, 3, &quot;Book&quot;};

    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&lt;Items&gt;::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.

答案1

得分: 0

std::find使用operator==比较元素,因此您需要为比较两个Items添加一个operator==重载:

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

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

或者简单地:

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

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

    // C++20版本:
    //auto operator<=>(const Items& o) const = default;
};
英文:

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&amp; o) const {
        return in_use == o.in_use
            &amp;&amp; item_no == o.item_no
            &amp;&amp; item_name == o.item_name;
    }
};

or simply

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

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

    // C++20 version:
    //auto operator&lt;=&gt;(const Items&amp; o) const = default;
};

</details>



huangapple
  • 本文由 发表于 2023年2月6日 15:30:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75358455.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定