Hey i set up a header and a .cpp file for my inventory. Im using a vector. I cant use anyof the push/ or pop methods that come in the vector library. I want to use them in the main. Also i am gettin 3 errors to do with the add method i made so that i could add to the vector in the main.
Can anyone help me to understand why i cant use the vectors functions and why i am getting these errors.
Here is my code:
Inventory.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
Inventory.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;
}
Errors
Error 1 error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 17 1 MaroonedCA2
Error 2 error C2061: syntax error : identifier 'string' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 17 1 MaroonedCA2
Error 3 error C2511: 'std::string Inventory::add(std::string)' : overloaded member function not found in 'Inventory' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 19 1 MaroonedCA2
Your issue is in the header, and you lack of
using namespace std. I never, ever use this command. I think it’s must better to be explicit with your namespaces, and you will avoid issues like this.If it was my project, I would remove the
using namespace stdand usestd::everywhere.Furthermore, you’re not using your class properly. You have member methods that are operating on global variables in the .cpp file.
Should be a a private member of the
Inventoryclass.Iterarors
To address your comments, what I’m trying to say is that you don’t need to declare iterators like that to use them, unless you need to store an iterator for later use (for some reason. Although can be dangerous when iterators are invalidated). Here are a few ways you can use them.
typedef
Using typedefs makes your life easier.
Now you can use them in loops.
auto
An easier way to do these loops, is with the
autokeyword. The compiler assign the appropriate type to the variable, which in this case isstd::vector<std::string>::iterator