C++ 结构和"expected primary-expression"错误

c++ struct and "expected primary-expression" error

本文关键字:primary-expression 错误 expected C++ 结构      更新时间:2023-10-16

用一些代码更容易解释:

#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中使用该函数来填充一个表,并且我需要在主函数中显示在doitow中插入的项目数。当我尝试调用函数时,我总是会出错:

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[]);

所以,这可能是一个明显的错误,但我是这方面的初学者。

谢谢你的帮助。

  1. test& info定义。如果将某些内容传递给函数,则编写一个类似info表达式。它是通过引用自动传递的,因为您在形式参数列表中指定了info作为引用。

    你还写了

    mytable[i] = info;
    

    而不是

    mytable[i] = test & info;
    

    不是吗?

    请改用insertdata(info);

  2. 数组也是如此。请改用doitnow(test)