【发布时间】:2019-03-22 18:27:25
【问题描述】:
我目前正在学习编程 2 课程 (c++),我们的任务是制作基于文本的 rpg。我使用这个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
【问题讨论】:
-
编译器如何知道如何“打印”
Item对象?您需要为您的Item结构重载operator<<函数。所有这些都应该在你正在阅读的书籍或教程中得到很好的解释,或者在你正在参加的课程中得到解释。与比较相同,编译器如何知道如何将Item对象与字符串进行比较? -
是的,关于那个...我的老师现在不在,他出事了,他通常不会向我们解释太多。我们也没有分配给我们阅读的任何编程书籍,也没有任何教程。
-
Here's a curated list of good books。我建议你读几本,它应该有助于你的老师没有解释的部分。