Inventor.h中矢量的添加方法出错

Error with add method for vector in Inventory.h

本文关键字:添加 方法 出错 Inventor      更新时间:2023-10-16

嘿,我为我的库存设置了一个头和一个.cpp文件。我在用矢量。我不能使用矢量库中的任何push/或pop方法。我想大体上使用它们。此外,我得到了3个错误与我所做的加法方法有关,这样我就可以在主向量中添加。

有人能帮助我理解为什么我不能使用向量函数,以及为什么我会得到这些错误吗。

这是我的代码:库存.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:usersconordocumentscollegedkit - year 2 - repeatdkit - year 2 - semester 1 - repeatgames programmingmaroonedca2maroonedca2inventory.h   17  1   MaroonedCA2
Error   2   error C2061: syntax error : identifier 'string' c:usersconordocumentscollegedkit - year 2 - repeatdkit - year 2 - semester 1 - repeatgames programmingmaroonedca2maroonedca2inventory.h   17  1   MaroonedCA2
Error   3   error C2511: 'std::string Inventory::add(std::string)' : overloaded member function not found in 'Inventory'    c:usersconordocumentscollegedkit - year 2 - repeatdkit - year 2 - semester 1 - repeatgames programmingmaroonedca2maroonedca2inventory.cpp 19  1   MaroonedCA2

string来自std命名空间

更改

std::string add(string item);

std::string add(std::string item);

有几个地方可以增强:

  1. 在Inventory.cpp中,最好不要使用using namespace std;,为std::vectorstd::string等变量提供完整的命名空间。

  2. vector<string> inventory;可以在库存类别内移动

您的问题在标题中,并且缺少using namespace std。我从不使用此命令。我认为显式使用名称空间一定更好,这样就可以避免类似的问题。

// Compiler knows what std::string is, but not string on it's own.
std:: string add(string item);

如果是我的项目,我会删除using namespace std并在任何地方使用std::

此外,你没有正确使用你的类。您有对.cpp文件中的全局变量进行操作的成员方法。

vector<string> inventory;

应该是Inventory类的私有成员

Iterarors

为了解决您的评论,我想说的是,您不需要声明这样的迭代器来使用它们,除非您需要存储迭代器以备日后使用(出于某种原因。尽管迭代器无效时可能会很危险)。以下是使用它们的几种方法。

typedef

使用typedefs可以让你的生活更轻松。

typedef std::vector<std::string> StringVec;
typedef StringVec::iterator StringVecIter;

现在您可以在循环中使用它们。

for(StringVecIter it = inventory.begin(); iter != inventory.end(); ++it)
{ ... }

自动

执行这些循环的一种更简单的方法是使用auto关键字。编译器为变量分配适当的类型,在本例中为std::vector<std::string>::iterator

for(auto it = inventory.begin(); it != inventory.end(); ++it)
{ ... }

去掉using namespace std;语句(您永远不应该使用它!)并正确限定您正在使用的名称空间项,例如:

库存.h

#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
class Inventory
{
public:
//Constructor
Inventory();
//Methods.
std::string add(std::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>
#include <algorithm>
std::vector<std::string> inventory;
Inventory::Inventory()
{
}
std::string Inventory::add(std::string item)
{
inventory.push_back(item);
return item;
}
void Inventory::showInventory()
{
char input[80];
std::cin >> input;
char inventoryRequest[] = "i";
//compare the player input to inventoryRequest (i) to see if they want to look at inventory.
if (strcmp(input, inventoryRequest) == 0)
{
displayInventory();
}
}
void displayInventoryItem(const std::string &item)
{
std::cout << item << std::endl;
}
void Inventory::displayInventory()
{
std::cout << "You have " << inventory.size() << " items." << std::endl;
std::cout << std::endl;
std::cout << "******Inventory******" << std::endl;
std::cout << "Your items:" << std::endl;
std::for_each(inventory.begin(), inventory.end(), displayInventoryItem);
}

将您的inventory、myIterator和Iter放入您的类inventory中。