输入/输出一个对象向量
Input/output a vector of objects
我有一堆代码,没有办法知道什么是错的,如何修复它。
这个程序需要一个类"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;
相关文章:
- 为什么我不能将一个对象push_back到属于另一个类的对象向量中?
- 从多个源构造一个对象,包括一个对象向量
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 为什么最后一个推回向量的对象的字段会转移到向量的其他对象?
- 如何在用户定义类型的向量上使用 vector::p ush_back() 来仅修改一个对象?
- 如何在C++中将一个对象从一个向量复制到另一个向量?
- 如何在C 中打印一个对象的2D向量
- 我如何访问一个对象的向量的成员变量,该对象是从类中的成员变量
- 将另一个对象传递到对象向量中
- 调用对象类方法,该方法在另一个对象类中的向量中
- 将向量从另一个对象添加到创建对象的对象
- 如何让一个对象在向量中时具有不同的参数类型?
- 包含另一个对象指针向量的对象
- 为什么我可以分配一个对象数组,但不能将它们放在向量中
- C++对一个对象向量进行排序,得到错误C3867函数调用缺少参数列表
- 从另一个字符串向量创建一个对象向量
- 输入/输出一个对象向量
- 从对象向量中删除一个对象
- 我如何使一个类包含一个向量的对象
- 我如何从一个类与字符的2D向量转换到另一个对象与字符变量的2D向量