Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;读入函数内部的结构数组?_C++_Arrays_Function_Struct - Fatal编程技术网

C++ C++;读入函数内部的结构数组?

C++ C++;读入函数内部的结构数组?,c++,arrays,function,struct,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 >&

我试图将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;
}
该错误出现在函数定义中。 我不知道如何修复此错误。如果有任何帮助,将非常感谢。

应该是:

  int addnewdep(department dep[], int d) {
     cin >> dep[d].id;
     cin >> dep[d].name;
  }
因为department是类型的名称,而不是函数的参数。另外,请在代码中额外注意

不需要在addnewdep()定义之前声明它。

它应该是:

  int addnewdep(department dep[], int d) {
     cin >> dep[d].id;
     cin >> dep[d].name;
  }
因为department是类型的名称,而不是函数的参数。另外,请在代码中额外注意


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

函数的定义中有一个
太多了。修复了这个问题,它最初不在我的代码中,所以仍然是相同的错误。
结构部门
是类型的名称。当它在声明中工作时,定义中需要一个参数名:
int addnewdep(结构部门[],int d){…}
应为
int addnewdep(结构部门部门[],int d){…}
如果希望在引用时将参数命名为
部门
。函数定义中有一个
太多了。修复了这个问题,它最初不在我的代码中,所以仍然是相同的错误。
结构部门
是类型的名称。虽然它在声明中工作,但在定义:
int addnewdep(struct department[],int d){…}
应该是
int addnewdep(struct department department[],int d){…}
如果您希望在引用该参数时将其命名为
department
。感谢您没有完全修复它,但当我将其更改为int addnewdep(struct department dep[],int d)时在定义和原型中,它修复了我的错误。谢谢。您仍然需要
struct关键字
struct department dep[]
。谢谢,这并没有完全修复它,但当我将它更改为int addnewdep(struct department dep[],int d)时在定义和原型中,它修复了我的错误。谢谢。您仍然需要
struct关键字
struct department dep[]