输入/输出一个对象向量

Input/output a vector of objects

本文关键字:向量 一个对象 输出 输入      更新时间:2023-10-16

我有一堆代码,没有办法知道什么是错的,如何修复它。

这个程序需要一个类"Item"来存储名称、价格等。第二个类必须制作项目列表,并具有打印、删除项目等选项。我还没有学习std::list,所以我使用向量。我得到的唯一错误是倒数第二行"' grocery '之前的预期主表达式"。

    class Item
    {
      protected: 
        string itemName, unit;     // (i.e. can, box, pounds, or ounces)
        double numberToBuy, unitPrice, extendedPrice;
      public:    
        Item();
        Item (string, string, double, double);
        string getName ();
        string getUnit();
        double getNumberToBuy();
        double getUnitPrice();
        double getExtendedPrice();
        void printItem();
    };
    class List
    {
      private: 
        Item item;
        int numberOfItems;
        vector <Item> groceries;
      public:     
        void addItem();
        void print();
    };
    void List::addItem()
    {  
      int stop;
      while (stop != 666) 
      { 
        cout << "Enter the name of your item "  << endl;
        string name;
        getline(cin, name);
        cin.ignore();
        cout << "Enter the units of your item "  << endl;
        string unit;
        getline(cin, unit);
        cout << "Enter the amount you would like to buy "<< endl;
        double amount;
        cin >> amount;
        cout << "Enter the price of your item " <<  endl;
        double price;
        cin >> price;
        //Item *item = new Item(name, unit, amount, price); 
        // ^^^I don't think I need this but I'm not quite sure.
        groceries.push_back(item);
        cin >> stop;
      }
    }
    void List::print()
    {
      auto v = vector<Item> groceries; 
      copy(begin(v), end(v), ostream_iterator<Item>(cout, " "));
    }

在Item类中,需要定义set类型函数,以便字符串和双精度成员数据可以获取值。到目前为止,您只有定义了返回值的get类型函数。

例如…

void setNumberToBuy(const double& val) {
    numberToBuy = val;
}

然后,在List::addItem()成员函数中,你需要调用那些set类型函数,以便为item对象提供数据…

cout << "Enter the amount you would like to buy " << endl;
double amount;
cin >> amount;
item.setNumberToBuy(amount);

…所以在最后,当你这样做的时候…

groceries.push_back(item);

…item对象将充满数据

这里是一个更完整的实现…

#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Item
{
private:
    string itemName{ "" }, unit{""};     // (i.e. can, box, pounds, or ounces)
    double numberToBuy{ 0.0 }, unitPrice{ 0.0 }, extendedPrice{ 0.0 };
public:
    Item() {}
    Item(string iN, string u, double nTB, double uP, double eP):
        itemName{ iN }, unit{ u }, numberToBuy{ nTB }, unitPrice{ uP }, extendedPrice{ eP } {
    }
    ~Item() {}
    // set functions
    void setName(const string& iN) {
        itemName = iN;
    }
    void setUnit(const string& u) {
        unit = u;
    }
    void setNumberToBuy(double nTB) {
        numberToBuy = nTB;
    }
    void setUnitPrice(double uP) {
        unitPrice = uP;
    }
    void setExtendedPrice(double eP) {
        extendedPrice = eP;
    }
    string getName();
    string getUnit();
    double getNumberToBuy();
    double getUnitPrice();
    double getExtendedPrice();
    void printItem();
    friend ostream& operator<<(ostream&^, const Item&);
};
ostream& operator<<(ostream& os, const Item& item) {
    os << itemName << " " << unit << " " << numberToBuy << " " << unitPrice << " " << extendedPrice << endl;
    return os;
}
class List
{
private:
    Item item;
    int numberOfItems;
    vector <Item> groceries;
public:
    void addItem();
    void print();
};
void List::addItem()
{
    int stop;
    double num{0.0};
    string str{""};
    while (stop != 666)
    {
        cout << "Enter the name of your item " << endl;     
        getline(cin, str);
        cin.ignore();
        item.setName(str);
        cout << "Enter the units of your item " << endl;        
        getline(cin, str);
        item.setUnit(str);
        cout << "Enter the amount you would like to buy " << endl;      
        cin >> num;
        item.setNumberToBuy(num);
        cout << "Enter the price of your item " << endl;        
        cin >> num;
        item.setUnitPrice(num);
        cout << "Set the extended price " << endl;
        cin >> num;
        item.setExtendedPrice(num);
        groceries.push_back(item); // now it's full of data
        cin >> stop;
    }
}
void List::print()
{
    auto v = vector<Item> groceries;
    copy(begin(v), end(v), ostream_iterator<Item>(cout, " "));
}

注意好友ostream&operator<& lt;