gpt4 book ai didi

c++ - 错误 c2678 : binary '=='

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:42:04 24 4
gpt4 key购买 nike

完整错误:

错误 2 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Item' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 1734 1 GameStore


库存类(cpp 文件)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include "item.h"

class Inventory
{


public:

void Inventory::viewInventory()
{
for (int i = 0; i < inventory.size(); i++)
{
Inventory::inventory[i].getItemName();
}
}

void Inventory::addItem(Item& item)
{
inventory.push_back(item);
}


void Inventory::removeItem(Item& item)
{
if (std::find(inventory.begin(), inventory.end(), item) != inventory.end())
{
inventory.erase(std::remove(inventory.begin(), inventory.end(), item), inventory.end());
}

else
{
std::cout << "Item does not exist" << std::endl;
}
}

//Player Gold methods
int Inventory::getGold()
{
return playerGold;
}

void Inventory::setGold(int newGold)
{
playerGold = newGold;
}


int Inventory::addGold(int newGold)
{
playerGold += newGold;
return playerGold;
}

int Inventory::removeGold(int newGold)
{
playerGold -= newGold;
return playerGold;
}
//Player Gold methods




private:
std::vector<Item> inventory = {};
int playerGold;

};

基本上我想做的是制作一个库存系统,用于保存“Item”类中的对象。我花了很长时间让我的 for 循环和我所有的工作方法,然后由于天空从未如此清晰,我得到了一个错误,这是我无法解决的问题。


元素等级 (CPP)

#include "item.h"
#include <iostream>
#include <string>


Item::Item(int id, std::string name, std::string description, std::string examime)
{
itemName = name;
itemID = id;
itemDescription = description;
itemExamine = examime;

}

void Item::setItemName(std::string newName)
{
itemName = newName;
}

void Item::setItemDescription(std::string newDescription)
{
itemDescription = newDescription;

}

void Item::setItemExamine(std::string newExamine)
{
itemExamine = newExamine;
}

void Item::setItemID(int newID)
{
itemID = newID;
}

std::string Item::getItemName()
{
return itemName;
}

std::string Item::getItemDescription()
{
return itemDescription;
}

std::string Item::getItemExamine()
{
return itemExamine;
}

int Item::getItemID()
{
return itemID;
}

项目类(标题)

#include <iostream>
#include <string>

class Item
{
public:
//constructor
Item::Item(int id, std::string name, std::string description, std::string examime);

//setters
void setItemName(std::string newName);
void Item::setItemDescription(std::string newDescription);
void Item::setItemExamine(std::string newExamine);
void Item::setItemID(int newID);

//getters
std::string Item::getItemName();
std::string Item::getItemDescription();
std::string Item::getItemExamine();
int Item::getItemID();

private:
std::string itemName;
int itemID;
std::string itemDescription;
std::string itemExamine;

};

如果您有任何建议,即使是重做我的整个系统,那就太好了。这些类(class)显然非常简单,我将添加更多内容。意思是我不是在寻找像“你甚至不需要 Item 类”这样的答案

感谢您的帮助!

最佳答案

您的代码的“核心”问题似乎是缺少 operator==为您的 Item 实现类,正如编译器错误告诉您的那样。

作为 C++ 初学者(不用担心:我们每个人都是初学者),您可能会问为什么是这个 op==必需,因为您没有在代码中显式 调用它。好吧,您确实没有显式调用它,但标准库实现 中的代码会调用它。特别是,您调用 std::find (在您的错误消息中引用的 <algorithm> header 中实现):

void Inventory::removeItem(...)
{
if (std::find(inventory.begin(), inventory.end(), item) != inventory.end())
{
...

std::find需要比较Item带有 op== 的实例.因此,要尝试修复您的代码,请定义一个 operator==用于比较 Item s,像这样:

// In the Item class header:

class Item
{
...
};

inline bool operator==(const Item& a, const Item& b)
{
// Implement your comparison logic for Items a and b.
// ...
// Return true if "a == b".
}

作为附加说明,当您在类中声明成员函数时,您不需要使用 Item::成员函数前的前缀:

class Item
{
public:
//constructor
Item::Item(int id, std::string name, std::string description, std::string examime);

//setters
void setItemName(std::string newName);
void Item::setItemDescription(std::string newDescription);
void Item::setItemExamine(std::string newExamine);
void Item::setItemID(int newID);
...

只是做:

class Item
{
public:
//constructor
Item(int id, std::string name, std::string description, std::string examime);

//setters
void setItemName(std::string newName);
void setItemDescription(std::string newDescription);
void setItemExamine(std::string newExamine);
void setItemID(int newID);

...

此外,考虑让你的 getters const , 使你的类常量正确,例如:

   class Item 
{
public:
...
std::string getItemName() const;
std::string getItemDescription() const;
std::string getItemExamine() const;
int getItemID() const;

...
};

关于c++ - 错误 c2678 : binary '==' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46723969/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com