使用某些代码更容易解释:
#include <iostream>
using namespace std;
struct test {
int one;
int two;
};
void insertdata(test & info)
{
cin >> info.one;
cin >> info.two;
}
int doitnow(test mytable[])
{
test info;
int i = 0;
for (i=0; i<3; i++) {
insertdata(test & info);
mytable[i] = info;
}
return i;
}
int main()
{
int total;
test mytable[10];
total = doitnow(test mytable[]);
cout << total;
}
所以,我需要通过引用函数insertdata传递信息,我需要在doitnow中使用该函数来填充表格,我需要在main函数中显示在doitnow中插入的项目数。当我尝试调用函数时,我不断收到错误:
teste.cpp: In function ‘int doitnow(test*)’:
teste.cpp:21:29: error: expected primary-expression before ‘&’ token
insertdata(test & info);
teste.cpp: In function ‘int main()’:
teste.cpp:33:30: error: expected primary-expression before ‘mytable’
total = doitnow(test mytable[]);
所以,可能这是一个明显的错误,但我是初学者。
感谢您的帮助。
答案 0 :(得分:0)
test& info
是定义。如果您将某些内容传递给某个函数,则可以编写一个表达式,如info
。它是自动通过引用传递的,因为您在形式参数列表中指定了info
作为引用。
您还写过
mytable[i] = info;
和不
mytable[i] = test & info;
没有?
改为使用insertdata(info);
。
数组也是如此。请改用doitnow(test)
。