c ++ struct和“expected primary-expression”错误

时间:2015-11-25 15:48:49

标签: c++

使用某些代码更容易解​​释:

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

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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

    您还写过

    mytable[i] = info;
    

    mytable[i] = test & info;
    

    没有?

    改为使用insertdata(info);

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