簡體   English   中英

嘗試在頭文件中私下聲明矢量時發生錯誤

[英]error when trying to declare a vector in private on a header file

嘿,建議我在private(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

庫存

#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

庫存文件

#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;
}

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

這與錯誤無關,但您也應避免在頭文件中using namespace std using namespace消除擁有名稱using namespace的優點,並且通過將其放入標頭中,可以將其強制應用於包含標頭的任何代碼。 這可能會以神秘的方式破壞代碼。 就個人而言,我不會在任何地方using namespace std 患疾病的可能性遠遠大於(相當適度的)好處。

您需要在頭文件中#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;
}

使長話短說:

#include <vector>

到您的.h文件中,一切正常。

您需要包括

#include <vector>

在您的頭文件中。 另外,在頭文件中using namespace std也是一種不好的做法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM