繁体   English   中英

C++ 没有运算符 [] 匹配这些操作数

[英]C++ No operator [] matches these operands

对不起,如果这是一个菜鸟错误,我真的是 C++ 的新手。 我的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();
}

收入信息结构:

struct incomeInfo {
string name;
double pay;
double healthInsuranceDeduction;
};

和电话:

incomeInfo employees[NUM_EMPS];

我得到的错误信息是No operator [] matches these operands; operands types are incomeInfo[int] No operator [] matches these operands; operands types are incomeInfo[int] 我正在传递一个int 谢谢!

你声明你的函数是错误的,你需要一个数组或指针,而incomeInfo只是一个结构,所以你不能使用incomeInfo[i].name 这是应该起作用的东西,注意大小写名称:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM