Tl; dr我有一堆代码,没有办法知道错误是什么以及如何解决它。
这个程序需要有一个"项目"存储名称,价格等。第二类必须列出项目并可以选择打印,删除项目等。我还没有学过std :: list所以我使用向量。我得到的唯一错误是"在杂货'"之前预期的主要表达。在倒数第二行。
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, " "));
}
答案 0 :(得分:1)
在Item类中,您需要定义 set 类型函数,以便字符串和双成员数据可以获取值。截至目前,您只能定义获取类型的函数来返回值。
例如......
void setNumberToBuy(const double& val) {
numberToBuy = val;
}
然后,在List :: addItem()成员函数中,您需要调用那些 set 类型函数,以便为项目对象提供数据...
cout << "Enter the amount you would like to buy " << endl;
double amount;
cin >> amount;
item.setNumberToBuy(amount);
......所以最后,当你这样做时......
groceries.push_back(item);
......项目对象将充满数据。
答案 1 :(得分:0)
这是一个更完整的实施......
#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&amp;运营商LT;&LT;在Item中添加的函数使List :: print函数有效。