[英]C++ No operator [] matches these operands
Sorry if this is a noob mistake, I'm really new to C++.对不起,如果这是一个菜鸟错误,我真的是 C++ 的新手。 My
cin
is not taking the value I'm trying to pass it.我的
cin
没有采用我试图传递的值。
void getData(incomeInfo incomeInfo, const int NUM_EMPS) {
for (int i = 0; i < NUM_EMPS; i++) {
cout << "Employee #" << i + 1 << "'s name: " << endl;
cin >> incomeInfo[i].name;
cout << endl;
cin.ignore();
}
The incomeInfo structure:收入信息结构:
struct incomeInfo {
string name;
double pay;
double healthInsuranceDeduction;
};
And the call:和电话:
incomeInfo employees[NUM_EMPS];
The error message I get is No operator [] matches these operands; operands types are incomeInfo[int]
我得到的错误信息是
No operator [] matches these operands; operands types are incomeInfo[int]
No operator [] matches these operands; operands types are incomeInfo[int]
. No operator [] matches these operands; operands types are incomeInfo[int]
。 I'm passing it an int
.我正在传递一个
int
。 Thanks!谢谢!
You're declaring your function wrong, you need an array or pointer and incomeInfo
is just a structure so you cannot use incomeInfo[i].name
.你声明你的函数是错误的,你需要一个数组或指针,而
incomeInfo
只是一个结构,所以你不能使用incomeInfo[i].name
。 Here's something that should work, pay attention to the upper and lower case names:这是应该起作用的东西,注意大小写名称:
struct IncomeInfo
{
string name;
double pay;
double healthInsuranceDeduction;
};
void GetData(IncomeInfo* incomeInfo, const int count)
{
for (int i = 0; i < count; i++)
{
cout << "Employee #" << i + 1 << "'s name: " << endl;
cin >> incomeInfo[i].name;
cout << endl;
cin.ignore();
}
}
void main()
{
IncomeInfo employees[NUM_EMPS];
GetData(employees, NUM_EMPS);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.