嘿,我正在使用 vector 及其迭代器。我将它与 Inventory.cpp 一起放在 Inventory.h 文件中。我想知道我可以直接调用它,这样我就可以访问 vector 库函数,例如push,pop等...因为目前我不能。有人可以帮我解决这个问题吗?
这是我的代码:
库存.h
//-------------------------------------------------------------------------------
// Inventory.h
//-------------------------------------------------------------------------------
#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
#include <vector>
using namespace std;
class Inventory
{
public:
//Constructor
Inventory();
//Methods.
string add(string item);
void displayInventory();
void showInventory();
private:
//Data members
vector<string> inventory;
vector<string>::iterator myIterator;
vector<string>::const_iterator iter;
};
#endif //INVENTORY_H
库存.cpp
#include "Inventory.h"
#include <iostream>
#include <vector> // To enable the use of the vector class.
#include <string>
using namespace std;
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;
}
我想要做什么:
int main()
{
Inventory inventory;
inventory.push_back();
}
错误
Error 2 error LNK2019: unresolved external symbol "public: class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > & __thiscall Inventory::GetContainer(void)" (?GetContainer@Inventory@@QAEAAV?$vector@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@V?$allocator@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@2@@std@@XZ) referenced in function _main C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\MaroonedCA2\Main.obj MaroonedCA2
Error 3 error LNK1120: 1 unresolved externals C:\Users\Conor\Documents\College\DKIT - Year 2 - Repeat\DKIT - Year 2 - Semester 1 - Repeat\Games Programming\MaroonedCA2\Debug\MaroonedCA2.exe MaroonedCA2
最佳答案
一个简单的解决方案是添加 push_back
以及您想要复制到 list 类中的其他 vector 方法:
class Inventory
{
public:
//Constructor
Inventory();
//Methods.
std::string add(std::string item);
void displayInventory();
void showInventory();
void push_back(const std::string& s) { inventory.push_back(s); }
private:
//Data members
std::vector<std::string> inventory;
std::vector<std::string>::iterator myIterator;
std::vector<std::string>::const_iterator iter;
};
请注意,从 std::vector
私有(private)继承也可能是一种选择。它不是 100% 安全,但很难找到可能造成问题的场景。公开继承是绝对不行的。此示例演示如何公开 std::vector
私有(private)继承的公共(public)接口(interface)的一部分:
class Inventory : private std::vector<std::string>
{
public:
// make a selection of the vector's methods public for this class.
using std::vector<std::string>::push_back;
using std::vector<std::string>::pop_back;
using std::vector<std::string>::begin;
using std::vector<std::string>::end;
};
关于c++ - 访问Inventory.h中的迭代器、 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13530382/