Ошибка при указании вектора в моем Inventory.h

Эй, я пытаюсь вставить мой вектор в inventory.h, чтобы у меня были все функции векторов в основном, такие как push_back, pop и т. Д. Вместо того, чтобы печатать все функции, такие как pop, push, я хочу, чтобы указатель работал но я получил ошибки. Может ли кто-нибудь помочь мне с этим и сказать мне, я иду по правильному пути с этим.

Заранее спасибо.

Вот мой код:

Inventory.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

inventory.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;
}

main.cpp

int main()
{
inventory.GetContainer().push_back("Stone");
}

Вот ошибки:

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

0

Решение

Похоже, реализация Inventors::GetContainer() пропал, отсутствует. Вы должны положить это в .cpp файл или встроенный в Inventory объявление класса.

в .cpp файл:

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

Хотя будьте осторожны, вы выставляете личный член данных. Вы могли бы также сделать участника публичным, если вы собираетесь это сделать. Таким образом, у вас нет иллюзий, что у вас есть какая-то форма инкапсуляции.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector