C++ 指向清单中的向量时出错。h

C++ 指向清单中的向量时出错。h,c++,vector,header,C++,Vector,Header,嘿,我试着在inventory.h中找到我的向量,这样我就有了所有的向量功能,比如push_back,pop等等。。。。我想要一个指向work的指针,而不是像pop那样键入所有函数,但是我遇到了错误。有谁能帮我一下,告诉我这条路走对了吗 提前谢谢 这是我的密码: 存货.h //------------------------------------------------------------------------------- // Inventory.h //-------------

嘿,我试着在inventory.h中找到我的向量,这样我就有了所有的向量功能,比如push_back,pop等等。。。。我想要一个指向work的指针,而不是像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();
    //vector<string> &GetContainer();
private:
    //Data members
   vector<string> inventory;
   vector<string>::iterator myIterator;
   vector<string>::const_iterator iter;
    };


#endif //INVENTORY_H
以下是错误:

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
错误2错误LNK2019:未解析的外部符号“public:class std::vector&u thiscall Inventory::GetContainer(void)”(?GetContainer@Inventory@@QEAAV$vector@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@std@@V$allocator@V?$basic_string@DU?$char_traits@D@性病病毒$allocator@D@2@@@std@@@2@@@std@@@XZ)函数\u main C:\Users\Conor\Documents\College\DKIT-2年级-Repeat\DKIT-2年级-第1学期-Repeat\Games Programming\MaroonedCA2\MaroonedCA2\main.obj MaroonedCA2中引用
错误3错误LNK1120:1未解析的外部C:\Users\Conor\Documents\College\DKIT-2年级-Repeat\DKIT-2年级-1学期-Repeat\Games Programming\MaroonedCA2\Debug\MaroonedCA2.exe MaroonedCA2

似乎缺少
Inventors::GetContainer()
的实现。您必须将其放在
.cpp
文件中,或内联在
清单
类声明中

.cpp
文件中:

vector<string>& Inventory::GetContainer() { return inventory; }
vector&Inventory::GetContainer(){return Inventory;}

尽管要小心,但您正在公开一个私有数据成员。如果你要这样做,你还不如把会员公之于众。这样,您就不会误以为您有某种形式的封装。

为什么要在.cpp文件中包含向量和字符串,只要您包含“inventory.h”,那么它就会包含向量和字符串。@请原谅,但这样做不会有任何损失。事实上,有一些好处:如果由于某种原因,头不再需要
std::vector
,并且您从中删除了include,那么.cpp文件仍然可以很好地编译。哦,我明白了。我到底应该在.cpp中输入什么。因为我实际上不需要任何东西,因为它是一个指针。我认为这会起作用:vector Inventory::GetContainer(){}@Pendo826通过引用返回向量。我添加了必要的行。在C++中,魔术不会发生任何事情,你必须在大部分时间内明确表示出来。谢谢你,我忘记了。有点滞后,哈哈。但是谢谢。
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
vector<string>& Inventory::GetContainer() { return inventory; }