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

Trying to pass an array of structures but I get a "cannot convert 'struct' to 'struct*' error

本文关键字:结构 转换 错误 数组      更新时间:2023-10-16

当我尝试将此结构数组传递到函数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]);
}

错误

当您在main()中调用函数时:

AthleticContest(subject[2]);

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

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

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

解决方案

为了摆脱此错误,解决方案是将指针传递给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

但是,要非常小心,因为您的函数具有非常危险的设计:您希望参数是指向至少包含 3 个连续元素的数组的指针。 因此,如果您用&subject[8]调用它,它将尝试访问超出界限的subject[10]。 如果你用&subject[2]调用,它将处理 garbege 信息,因为你只启动了前两个元素,而不是第 3、4 和 6 个元素。

更好的解决方案

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

main()

AthleticContest(subject, 2);  // 2 is the number of contestants in array

您的函数将定义为:

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++)
...
}

更好的解决方案

你最好选择std::vector而不是C++数组。它们的行为可以像数组一样:

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

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

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

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

int AthleticContest(vector<person> &subjects)
{
...
}

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

for (int hh = 0; hh < subjects.size(); hh++)
...

这是一个在线演示。

当然,如果你不想把向量中的所有参与者都包括在内,你必须考虑你的函数的参数:你更喜欢第二个参数n并总是取向量的n个第一个元素吗?或者您是否更喜欢 n 个参与者但从任意偏移量开始?在这两种情况下,明智的做法是检查索引是否永远不会越界。

编译时需要更改的一件事:您正在传递对数组的单个元素的引用,而不是数组本身。

您可能要检查的另一件事是更改AthleticContest()的签名,以便C++接收固定大小的数组或person作为参数是正确的。

当固定为编译时,你的代码看起来像这样:

#include <iostream>
using namespace std;
struct person
{
int athletic;
int smarts;
int spirit;
int contestVal;
};
template <std::size_t size>
int AthleticContest(person (&subjects)[size])
{
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 (5 > 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);
}