所以我想做一个使用类项目向量的库存程序。到目前为止,我的代码看起来像这样:
#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;
}
程序无法编译,因为getdata()
万一 8 没有调用该函数,这是为什么呢?另一个问题是,如果我在 getdata() 函数中存储了数据,我如何通过取消引用在其他函数中使用该数据向量数据?