Inventor.h中矢量的添加方法出错
Error with add method for vector in Inventory.h
嘿,我为我的库存设置了一个头和一个.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);
有几个地方可以增强:
在Inventory.cpp中,最好不要使用
using namespace std;
,为std::vector
、std::string
等变量提供完整的命名空间。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中。
- 在另一个类视图中添加最多2个图表的正确方法是什么
- 将 c++ 包含添加到日食的正确方法
- 有什么方法可以将元素添加到列表中,如图所示?
- 在QT中为小部件添加彩色边框的最佳方法是什么
- 计算机使用什么方法添加无符号整数
- 为什么 memcpy() 是一种将元素添加到 'std::map' 的方法?
- 如何将信号处理程序添加为方法
- 添加具有模板专用化的方法
- 将动态方法添加到类中:使用宏函数到头文件中
- 向size_t添加 int 的正确方法
- 在犰狳立方体中添加一列 1 的有效方法
- 是否有类周围最薄的包装器来添加方法?
- 在从抽象基类继承的类中添加方法
- Inventor.h中矢量的添加方法出错
- 如何将常量传递给我的动态数组类添加方法
- C++:在不违反SRP的情况下向多态类层次结构添加方法
- 如何在c++中动态地向类添加方法
- 如何实现重叠添加方法
- 当派生指针仅添加方法时,将基指针强制转换为派生指针的有效性
- 为每个模板(int)形参向类中添加方法