gpt4 book ai didi

C++ 基于文本的 RPG 库存系统

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:12:27 27 4
gpt4 key购买 nike

我目前正在上编程 2 类(class) (c++),我们的任务是制作一个基于文本的角色扮演游戏。我正在使用这个 post作为我的库存系统的引用,因为我认为它非常有效。但我一直遇到 E0349“没有运算符“==”或“<<”匹配这些操作数”错误。

如果有人能帮助我,那就太好了。这是我的全套代码:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <ostream>
#include <Windows.h>
#include <string>
#include <cctype>
using namespace std;

struct Item {
string name; //Item name.
int slot; //Head, Torso, Hands
int attack;
int knowledge;
int defense;
int hp;
int speed;
int charisma;
};

int main()
{

//Variables, Strings, etc.
int itemcounter = 0, counter = 0;


//"Empty" Item
Item Empty{ "<Empty>", 0, 0, 0 };

vector<Item> Equipment = { 6, Empty }; //Current Equipment, 6 empty slots.
vector<Item> Inventory = { }; //Player Inventory.
string InventorySlots[] = { "Head" "Torso", "Hands" }; //Player parts where items can be equiped.

cout << "You sit your bag down and take a look inside." << " You have:" << endl;

for (int i = 0; i < itemcounter; i++)
{
cout << InventorySlots[i];
if (Equipment[i] == "Empty ")
{
cout << " " << Equipment[i] << endl << endl;
}
}

}

这里是我的错误更具体的地方

for (int i = 0; i < itemcounter; i++)  //Display equipped
{
cout << InventorySlots[i];
if (Equipment[i] == "Empty ") //Error Here
{
cout << " " << Equipment[i] << endl << endl; //Errore Here
}
}

错误信息

Error (active)  E0349   no operator "<<" matches these operands     C:\Users\USER\source\repos\clunkinv\clunkinv\clunkinv.cpp   47

最佳答案

Equipment[i]Item 类型的对象。如果您不提供将您的对象与 "Empty" 进行比较的方法,编译器将不知道如何在行中进行比较

if (Equipment[i] == "Empty ")

要么你比较属性

if (Equipment[i].name == "Empty ")

或者你必须提供一个方法。同线问题

cout << " " << Equipment[i] << endl << endl;

编译器不知道如何打印你的对象。您必须为此提供一个函数。

你可以

struct Item {
string name; //Item name.
int slot; //Head, Torso, Hands
int attack;
int knowledge;
int defense;
int hp;
int speed;
int charisma;
};

std::ostream &operator<<(std::ostream &os, const Item& item) {
os << item.name;
return os;
}

如果你想使用它们,你必须为你的类重载运算符。

关于C++ 基于文本的 RPG 库存系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55279901/

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