1

所以,我有这个结构:

typedef struct {
    int day;
    int amount;
    char type[4],desc[20];
 }transaction;

这个函数用于填充类型为transaction的向量,在 main 中声明:

void transact(transaction t[],int &n)
 {
    for(int i=0;i<n;i++)
    {
        t[i].day=GetNumber("Give the day:");
        t[i].amount=GetNumber("Give the amount of money:");
        t[i].type=GetNumber("Give the transaction type:");
        t[i].desc=GetNumber("Give the descripition:");
    }
 }

我在函数标题处得到的错误transact()

Multiple markers at this line
- Syntax error
- expected ';', ',' or ')' before '&' token

4

2 回答 2

6

您正在尝试将n参数声明为引用 ( int &n)。但是引用是 C++ 的特性,在 C 中不存在。

由于该函数不会修改 的值n,因此只需将其设为普通参数即可:

void transact(transaction t[],int n)

稍后您在尝试分配数组时也会遇到错误:

t[i].type=GetNumber("Give the transaction type:");
t[i].desc=GetNumber("Give the descripition:");

由于GetNumber可能返回一个数字,因此不清楚您要在那里做什么。

于 2013-03-18T18:08:38.850 回答
3

C++ 有诸如int &n;之类的引用。C 没有。

删除&.

然后,您将在为 和 分配数字时遇到t[i].type问题t[i].desc。它们是字符串,你不能像那样分配字符串,你可能应该使用类似的东西void GetString(const char *prompt, char *buffer, size_t buflen);来进行读取和分配:

GetString("Give the transaction type:", t[i].type, sizeof(t[i].type));
于 2013-03-18T18:09:01.667 回答