loops - 在 for 循环的输出中,它显示为零并且不接受输入我无法弄清楚我在这里做错了什么
问题描述
#include <iostream>
#include <exception>
using namespace std;
class Inventory{
friend istream& operator>>(istream&, Inventory);
friend ostream& operator<<(ostream&, Inventory);
private:
int stock;
int quant;
int price;
public:
Inventory(int=0,int=0,double=0);
};
Inventory::Inventory(int s, int q, double p){
stock=s;
quant=q;
price=p;
}
istream& operator>>(istream& in, Inventory){
int s;
int q;
double p;
cout << "Enter stock number: ";
in >> s;
cout << "Enter quantity: ";
in >> q;
cout << "Enter price: ";
in >>p;
Inventory inv(s,q,p);
return in;
}
ostream& operator<<(ostream& out, Inventory inv){
out << "The stock number is: " << inv.stock << endl << "The quantity is: " <<
inv.quant << endl << "The price is: " << inv.price << endl;
return out;
}
int main()
{
Inventory inv[5]={};
for(int i = 0; i <5; i++){
cin >> inv[i];
}
cout<<"------------------------" << endl;
for(int i=0;i<5;i++){
cout<<inv[i];
cout<<"------------------------" << endl;
}
return 0;
}
我无法弄清楚为什么当我在 for 循环中使用重载的 << 运算符时它显示为零,我是否没有将价格、数量和价格正确存储到对象数组中?我尝试过不使用构造函数并使用它没有任何区别,任何想法为什么整数和双精度不存储到数组中?
解决方案
你很接近!在下面的代码中查看我的// change here注释标记(有几个)。
0 与 << 重载无关,而是与 >> 重载有关。
交易是您可以在 >> 重载中使用“in”istream 直接输入类变量(无需尝试调用其他函数或需要额外的变量),但您需要将 Inventory 引用传递给这样做。
我很惊讶在构造函数参数中尝试将原语设置为 0 没有错误??(我可能对较新的 C++ 处理非常“生疏”,对我来说已经超过 15 年了),但无论如何:
#include <iostream>
#include <exception>
using namespace std;
class Inventory{
friend istream& operator>>(istream&, Inventory&); // change here (Inventory reference)
friend ostream& operator<<(ostream&, Inventory);
private:
int stock;
int quant;
double price; // change here
public:
Inventory(int,int,double); // change here
};
Inventory::Inventory(int s = 0, int q = 0, double p = 0){ // change here
stock=s;
quant=q;
price=p;
}
istream& operator>>(istream& in, Inventory& inv){ // change here (inventory reference)
// change here (removed variables)
cout << "Enter stock number: ";
in >> inv.stock; // change here
cout << "Enter quantity: ";
in >> inv.quant; // change here
cout << "Enter price: ";
in >> inv.price; // change here
// change here (removed function call)
return in;
}
ostream& operator<<(ostream& out, Inventory inv){
out << "The stock number is: " << inv.stock << endl << "The quantity is: " <<
inv.quant << endl << "The price is: " << inv.price << endl;
return out;
}
int main()
{
const int ITEMS = 5; // change here (constant to replace hard-coded value)
Inventory inv[ITEMS]={}; // change here (use the constant)
for(int i = 0; i <ITEMS; i++){ // change here (use the constant)
cin >> inv[i];
}
cout<<"------------------------" << endl;
for(int i=0;i<ITEMS;i++){ // change here (use the constant)
cout<<inv[i];
cout<<"------------------------" << endl;
}
return 0;
}
试一试,看看结果如何:
这是我将常量(我在主函数中的更改)更改为 2 以进行基本测试运行时的示例输出:
Enter stock number: 123
Enter quantity: 4
Enter price: 2.1
Enter stock number: 467
Enter quantity: 1
Enter price: 1.2
------------------------
The stock number is: 123
The quantity is: 4
The price is: 2.1
------------------------
The stock number is: 467
The quantity is: 1
The price is: 1.2
------------------------
推荐阅读
- html - 如何将 html 元素缩放到绝对大小,而不是通过因子
- python - 为什么空 Pandas 数据框的一列的总和返回布尔值
- python - 如何创建具有平滑边界的球体图像
- c# - 如何处理多个 ASP.NET WebForms 提交
- javascript - React Promise 异步任务顺序不正确
- c# - 是否有执行与 Add-Migration 相同功能的 EF Core 包管理器控制台命令
EF6 中的 -IgnoreChanges 命令 - r - R函数中递归`list.files()`调用的有效策略
- java - 在数据库中保存“时间”变量的问题
- android - 线性布局超出范围
- python - 运行 setup.py 时以 '\xff' 开头的非 UTF-8 代码