0

嘿,我被建议在私有(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;
}
4

4 回答 4

3

您需要包含<vector>在您的Inventory.h文件中。

这与错误无关,但您也应该避免放入using namespace std头文件。using namespace否定了拥有命名空间的优势,并且通过将其放在标头中,您将其强制用于包含您的标头的任何代码。这可能会以神秘的方式破坏代码。就个人而言,我不会using namespace std在任何地方使用。混乱的可能性远远超过(相当适度的)好处。

于 2012-11-22T09:02:28.187 回答
2

你需要#include <vector>在头文件中

#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

在 Inventory.cpp 中,还为字符串提供命名空间

std::string Inventory :: add(std::string item)
{
  inventory.push_back(item);
  return item;
}
于 2012-11-22T09:02:25.930 回答
1

为了使长的事情变短:

#include <vector>

进入您的 .h 文件,一切正常。

于 2012-11-22T09:02:29.343 回答
1

你需要包括

#include <vector>

在你的头文件中。在头文件中做using namespace std也是不好的做法。

于 2012-11-22T09:03:37.040 回答