在 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 循环中使用重载的 <<运算符时它显示零,我没有将价格、数量和价格正确存储到对象数组中吗?我试过不使用构造器并且使用构造器没有区别,任何想法为什么整数和双精度不存储到数组中?

你很接近! 在下面的代码中查看我的//在此处更改注释标记(有几个(。

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;
}

试一试,看看进展如何:

这是我在基本测试运行中将常量(我在 main 函数内的更改(更改为 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
------------------------

相关内容

最新更新