尝试传递结构数组,但出现“无法将 'struct' 转换为 'struct*' 错误

Cur*_*Guy 2 c++ arrays parameter-passing data-structures

当我尝试将此结构数组传递给函数时,我不断收到此错误AthleticContest()

无法将“人”转换为“人*”

有人可以告诉我我做错了什么吗?我是否将错误的东西传递给函数?

struct person
{
    int athletic;
    int smarts;
    int spirit;
    int contestVal;
};

int AthleticContest(person subjects[])
{
     cout << "Athletic Contest!!!" << endl << endl;
     for (int hh = 0; hh < 3; hh++)
     {
         int result = subjects[hh].athletic;
         subjects[hh].contestVal = result;
         cout << "Contestant # " << (hh+1) << ") " << subjects[hh].contestVal  << endl;
     }
     int winner;
     int tempWin = -1;
     for (int hh = 0; hh < 3; hh++)
     {
        if (subjects[hh].contestVal > tempWin)
        {
            tempWin = subjects[hh].contestVal;
            winner = hh;
        }
        else if (subjects[hh].contestVal == tempWin)
        {
            if (randomInt() > 4)
                winner = hh;
        }
    }
    cout << "Winner is Contestant # " << (winner+1)   << endl;

    return winner;
}

int main()
{
    person subject[10];

    subject[0].athletic = 5;
    subject[0].smarts   = 3;
    subject[0].spirit   = 1;
    subject[1].athletic = 1;
    subject[1].smarts   = 3;
    subject[0].spirit   = 5;
    subject[1].athletic = 3;
    subject[1].smarts   = 5;
    subject[0].spirit   = 1;

    AthleticContest(subject[2]);
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*phe 5

错误

当您在以下位置调用函数时main()

    AthleticContest(subject[2]);
Run Code Online (Sandbox Code Playgroud)

您传递一个 single 作为参数person,它是数组的第三个元素(索引为 2 的元素)。因此编译器会理解您尝试传递此类型的对象person

但是您的函数的参数被声明为未确定大小的数组类型(即person[])。C++ 将此类数组参数视为指针(指向其第一个元素),就像person*.

这就是您收到此错误消息的原因:这些类型不兼容

解决方案

要消除此错误,解决方案是将指针传递给 a subject,例如:

    AthleticContest(&subject[2]);  // passes the pointer to the third element
    // or 
    AthleticContest(subject);  // passes the pointer to the first element of the original array
Run Code Online (Sandbox Code Playgroud)

但是,要非常小心,因为您的函数有一个非常危险的设计:您希望参数是指向至少包含 3 个连续元素的数组的指针。因此,如果您使用 来调用它&subject[8],它会尝试访问subject[10]超出范围的内容。如果你调用&subject[2]它,它将处理垃圾信息,因为你只初始化了前两个元素,而不是第三个、第四个和第六个。

更好的解决方案

目前尚不清楚为什么只用 3 个元素进行比赛。更好的选择是调用者说出应使用多少参赛者(调用者知道数组的大小)。

main()

    AthleticContest(subject, 2);  // 2 is the number of contestants in array
Run Code Online (Sandbox Code Playgroud)

您的函数将定义为:

int AthleticContest(person subjects[], int participants)
{
     cout << "Athletic Contest!!!" << endl << endl;
     for (int hh = 0; hh < participants; hh++)
     ... 
     for (int hh = 0; hh < participants; hh++)
     ...
}
Run Code Online (Sandbox Code Playgroud)

更好的解决方案

你最好选择std::vectorC++ 数组而不是数组。它们的行为类似于数组:

    vector<person> subject(2);  // create a vector of 2 items
    subject[0].athletic = 5;
    ...
Run Code Online (Sandbox Code Playgroud)

但它们更方便,因为它们可以具有动态大小并随着您添加新参与者而增长:

    person hbolt = {4, 2, 1, 0}; 
    subject.emplace_back (hbolt);   // one more participant
    AthleticContest(subject);
Run Code Online (Sandbox Code Playgroud)

您的函数可以通过引用或按值获取向量。让我们参考一下,因为您打算修改其内容,并且可能在返回后需要修改后的数据:

int AthleticContest(vector<person> &subjects)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

最大的优点是你总是可以知道向量的大小:

for (int hh = 0; hh < subjects.size(); hh++)
...
Run Code Online (Sandbox Code Playgroud)

这是一个在线演示

当然,如果您不想获取向量中的所有参与者,则必须考虑函数的参数:您是否更喜欢第二个参数 n 并始终获取向量的第 n 个元素?或者您希望有 n 个参与者但以任意偏移量开始?在这两种情况下,明智的做法是检查索引是否超出范围。