Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/127.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数调用不';t工作和矢量访问数据_C++ - Fatal编程技术网

C++ 函数调用不';t工作和矢量访问数据

C++ 函数调用不';t工作和矢量访问数据,c++,C++,所以我想做一个库存程序,在其中使用类项目的向量。到目前为止,我的代码如下所示: #include <iostream> #include <vector> #include <string> using namespace std; class Item{ private: string month; string name; float volume; float sales; public: void print(); void p

所以我想做一个库存程序,在其中使用类项目的向量。到目前为止,我的代码如下所示:

#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Item{
private:
  string month;
  string name;
  float volume;
  float sales;
public:
  void print();
  void print2();
  void sale();
  void report();
  void getdata();
  void setname (string itemname){name=itemname;}
  void setID(int setID){ID=setID;}
};
void Item::print(){
  cout<<"Name    : "<<name<<endl;
  cout<<"ID      : "<<ID<<endl;
}
void Item::print2(){
  vector<Item>*::iterator i;
    for (i=list.begin(); i !=list.end(); ++i){
      i->print();
  }
/*void Item::sale(){
  vector<Item> }

void Item::report(){
}*/
void Item::getdata(){
  vector<Item> items;
  string name;
  int ID;
  Item *a;
  for (int n=0; n<2; n++){
    cout<<"Enter name:"<<endl;
    getline(cin, name);
    cout<<"Enter ID: "<<endl;
    cin>>ID;
    a = new Item;
    a->setname(name);
    a->setID(ID);
    items.push_back(*a);
    cin.get();
  }
}

int main(){
  cout<<"0. Exit \n1. Item Sale \n2. Daily Report \n3. Weekly Check \n4. Monthly Update \n5. Load Inventory File \n6. S\
ort file by Key \n7. Save inventory to a file \n8. Add a New Item \n9. Edit an Item \n10. Delete an item\n";
  int x;
  cin>>x;
  switch(x){
  case 0:
    exit(0);
    break;
  case 1:
    sale();
    break;
  case 2:
    break;
  case 8:
    getdata();
    break;
  default:
    cout<<"Wrong choice!\n";
    break;
  }
#包括
#包括
#包括
使用名称空间std;
类项目{
私人:
弦月;
字符串名;
浮子体积;
浮动销售;
公众:
作废打印();
void print2();
无效销售();
作废报告();
void getdata();
void setname(string itemname){name=itemname;}
void setID(int setID){ID=setID;}
};
无效项::打印(){

cout您需要修复OOP的基础知识。您不能在类项的main中创建对象。否则,您无法调用公共成员函数

在不创建类的对象的情况下,只能调用静态成员函数

其次,在成员函数中创建类本身的向量是不明智的。如果必须的话,可以在类之外创建

类定义一个实例的属性和行为。如果希望创建实例的数组/向量,则必须在类之外进行,然后每个实例调用一些成员函数


您应该创建类来处理一个没有向量的项。getdata函数还应该为一个项获取输入。您可以在main中创建一个向量。然后在此向量上运行一个循环,并为每个项调用get_item或任何其他api。要详细说明,请在循环中从控制台获取输入并将其传递给vector[index].setitem()

我建议将对象设计分为两部分:一部分表示项目,另一部分表示库存

项目:

现在,您可以在迭代时检查此值:

void Inventory::find
(
  const int ID_
) const
{
  bool found = false;
  for (
    vector<Item>::const_iterator iter = items.begin();
    iter != items.end();
    ++iter)
  {
    if (iter->getID() == ID_)
    {
      cout << "Found item " << ID_ << ": \n";
      iter->print();
      found = true;
    }
  }
  if (!found)
  {
    cout << "No items matching " << ID_ << " found." << endl;
  }
}
void Inventory::find
(
常量整数ID_
)常数
{
bool-found=false;
为了(
向量::常量迭代器iter=items.begin();
iter!=items.end();
++国际热核聚变实验堆(iter)
{
如果(iter->getID()==ID)
{

那么向量应该在类中声明吗?不应该在类外声明。类定义了对象的外观。现在使用同一个类来创建它自己的数组是非常令人困惑的。如果你能概述一下你想做什么,我可以帮你更好。基本上我想做的是一个清单程序,它应该能够添加类中列出的特定成员的新项。它应该使用向量来存储它,这样我就可以对项进行排序并保存它,然后将其打开到structuresOkay指针的大小数组中,因此您应该创建类来处理一个没有向量的项。getdata函数还应该为一个项获取输入在main中创建一个向量。然后在此向量上运行一个循环,并为这些项中的每一项调用get_item或任何其他api。要详细说明,在循环中,从控制台获取输入并将其传递给向量[index]。setitem()如何遍历向量以查找特定ID?例如,`int ID;coutid(vector::const_iterator iter=items.begin();iter!=items.end();++iter){if(id==id){iter->print();}}}`这似乎不起作用
class Inventory
{
public:
  void print() const;
  void report();
  void getdata();

private:
  std::vector<Item> items;
};

void Inventory::print() const
{  
  for (
    vector<Item>::const_iterator iter = list.begin();
    iter != items.end();
    ++iter)
  {
      iter->print();
  }
}
void Inventory::getdata()
{
  string name;
  int ID;
  for (int n=0; n<2; n++)
  {
    cout<<"Enter name:"<<endl;
    getline(cin, name);
    cout<<"Enter ID: "<<endl;
    cin>>ID;
    Item item;
    item.setname(name);
    item.setID(id);
    items.push_back(item);
    cin.get();
  }
}
int main()
{
  Inventory inventory;
  cout
      << "0. Exit \n"
      << "1. Item Sale \n"
      << "2. Daily Report \n"
      << "3. Weekly Check \n"
      << "4. Monthly Update \n"
      << "5. Load Inventory File \n"
      << "6. Sort file by Key \n"
      << "7. Save inventory to a file \n"
      << "8. Add a New Item \n"
      << "9. Edit an Item \n"
      << "10. Delete an item\n";
  int x;
  cin>>x;
  switch(x)
  {
  case 0:
    exit(0);
    break;
  case 1:
    sale();
    break;
  case 2:
    inventory.report();
    break;
  case 8:
    inventory.getdata();
    break;
  default:
    cout<<"Wrong choice!\n";
    break;
  }
}
int getID() const {return ID;}
void Inventory::find
(
  const int ID_
) const
{
  bool found = false;
  for (
    vector<Item>::const_iterator iter = items.begin();
    iter != items.end();
    ++iter)
  {
    if (iter->getID() == ID_)
    {
      cout << "Found item " << ID_ << ": \n";
      iter->print();
      found = true;
    }
  }
  if (!found)
  {
    cout << "No items matching " << ID_ << " found." << endl;
  }
}