C ++读入函数内部的struct数组?

时间:2016-05-27 20:13:36

标签: c++ arrays function struct

我试图将一个int读入一个结构数组,但是在' ['之前]我得到了一个预期表达式的错误。在尝试编译时。

struct department {
    int id;
    char name[20];
};

int addnewdep(struct department[],int d);
int main()
{
.....
}
int addnewdep(struct department[],int d)
{
    cin >> department[d].id;
    cin >> department[d].name;
}

错误出现在函数定义中。 我不知道如何解决这个错误。对此的任何帮助都会很棒,谢谢。

1 个答案:

答案 0 :(得分:2)

应该是:

  int addnewdep(department dep[], int d) {
     cin >> dep[d].id;
     cin >> dep[d].name;
  }

因为department是类型的名称,而不是函数的参数。另请注意额外的;在你的代码中。

addnewdep()的声明就在它的定义不是必需之前。