gpt4 book ai didi

c++ - 尝试在头文件中私有(private)声明 vector 时出错

转载 作者:太空宇宙 更新时间:2023-11-04 15:23:50 24 4
gpt4 key购买 nike

嘿,有人建议我在 private(Iventory.h) 中声明我的 vector ,而不是在我已经完成的 .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

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

库存.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;
}

最佳答案

您需要包括 <vector>在你的Inventory.h文件。

这与错误无关,但你也应该避免把using namespace std在头文件中。 using namespace否定了拥有 namespace 的优势,通过将它放在 header 中,您可以将它强制应用于包含您的 header 的任何代码。这可能会以神秘的方式破坏代码。就个人而言,我不会使用 using namespace std任何地方。困惑的可能性远远超过(非常适度的)好处。

关于c++ - 尝试在头文件中私有(private)声明 vector 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13508960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com