Reputation: 1002
Hey i was advised to declare my vector in private(Iventory.h) instead of having in globally in the .cpp(Inventory.cpp) which i had done. But now there are a lot of errors spitting out at me.
Here are the errors:
Error 1 error C2143: syntax error : missing ';' before '<' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 22 1 MaroonedCA2
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 22 1 MaroonedCA2
Error 3 error C2238: unexpected token(s) preceding ';' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 22 1 MaroonedCA2
Error 4 error C2143: syntax error : missing ';' before '<' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 23 1 MaroonedCA2
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 23 1 MaroonedCA2
Error 6 error C2238: unexpected token(s) preceding ';' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 23 1 MaroonedCA2
Error 7 error C2143: syntax error : missing ';' before '<' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 24 1 MaroonedCA2
Error 8 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 24 1 MaroonedCA2
Error 9 error C2039: 'const_iterator' : is not a member of '`global namespace'' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 24 1 MaroonedCA2
Error 10 error C2238: unexpected token(s) preceding ';' c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.h 24 1 MaroonedCA2
Error 11 error C2065: 'inventory' : undeclared identifier c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 18 1 MaroonedCA2
Error 12 error C2228: left of '.push_back' must have class/struct/union c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 18 1 MaroonedCA2
Error 13 error C2065: 'inventory' : undeclared identifier c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 39 1 MaroonedCA2
Error 14 error C2228: left of '.size' must have class/struct/union c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 39 1 MaroonedCA2
Error 15 error C2065: 'inventory' : undeclared identifier c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 42 1 MaroonedCA2
Error 16 error C2228: left of '.size' must have class/struct/union c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 42 1 MaroonedCA2
Error 17 error C2065: 'inventory' : undeclared identifier c:\users\conor\documents\college\dkit - year 2 - repeat\dkit - year 2 - semester 1 - repeat\games programming\maroonedca2\maroonedca2\inventory.cpp 43 1 MaroonedCA2
Inventory.h
#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
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
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;
}
Upvotes: 0
Views: 2080
Reputation: 45420
you need to #include <vector>
in header file
#ifndef INVENTORY_H
#define INVENTORY_H
#include <string>
#include <vector>
//using namespace std; <-- get rid of this line
//it's bad idea to include all std stuff into your code
class Inventory
{
public:
//Constructor
Inventory();
//Methods.
std::string add(std::string item);
void displayInventory();
void showInventory();
private:
//Data members
std::vector<std::string> inventory;
//^^^ use full namespace qualified variables
std::vector<std::string>::iterator myIterator;
std::vector<std::string>::const_iterator iter;
};
#endif //INVENTORY_H
in Inventory.cpp, also provide namespace for string
std::string Inventory :: add(std::string item)
{
inventory.push_back(item);
return item;
}
Upvotes: 2
Reputation: 227458
You need to include <vector>
in your Inventory.h
file.
This is not related to the error, but you should also avoid putting using namespace std
in header files. using namespace
negates the advantages of having namespaces, and by putting it in the header, you force it upon any code that includes your header. This could break code in mysterious ways. Personally, I would not use using namespace std
anywhere. The potential for disorder far outweighs the (quite modest) benefits.
Upvotes: 3
Reputation: 52539
You need to include
#include <vector>
in your header file. Also doing using namespace std
in header files is poor practice.
Upvotes: 1
Reputation: 2598
To make long things short:
Write
#include <vector>
into your .h file and everything should work fine.
Upvotes: 1