嘿,我被建议在私有(Iventory.h)中声明我的向量,而不是在我所做的.cpp(Inventory.cpp)中全局声明。但是现在有很多错误吐在我身上。
以下是错误:
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
库存.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
库存.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;
}