嘿,我为我的库存设置了一个标题和一个 .cpp 文件。我使用向量。我不能使用向量库中的任何 push/ 或 pop 方法。我想主要使用它们。我也遇到了与我所做的 add 方法有关的 3 个错误,以便我可以添加到 main 中的向量中。
谁能帮我理解为什么我不能使用向量函数以及为什么会出现这些错误。
这是我的代码: Inventory.h
#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
class Inventory
{
public:
//Constructor
Inventory();
//Methods.
std:: string add(string item);
void displayInventory();
void showInventory();
private:
//Data members
};
#endif //INVENTORY_H
库存.cpp
#include "Inventory.h"
#include <iostream>
#include <vector> // To enable the use of the vector class.
#include <string>
using namespace std;
vector<string> inventory;
vector<string>::iterator myIterator;
vector<string>::const_iterator iter;
Inventory::Inventory()
{
}
string Inventory :: add(string item)
{
inventory.push_back(item);
return item;
}
void Inventory:: showInventory()
{
char input[80];
cin >> input;
char inventoryRequest[] = "i";
int invent = strcmp (input,inventoryRequest);
//compare the player input to inventoryRequest (i) to see if they want to look at inventory.
if(invent == 0)
{
displayInventory();
}
}
void Inventory:: displayInventory()
{
//vector<string> inventory;
cout<< "You have " << inventory.size() << " items.\n";
cout << "\n******Inventory******";
cout<< "\nYour items:\n";
for (int i= 0; i< inventory.size(); ++i)
cout<< inventory[i] << endl;
}
错误
Error 1 error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 17 1 MaroonedCA2
Error 2 error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 17 1 MaroonedCA2
Error 3 error C2511: 'std::string Inventory::add(std::string)' : overloaded member function not found in 'Inventory' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 19 1 MaroonedCA2