尝试传递结构数组,但出现"无法将'结构'转换为'结构*'错误
Trying to pass an array of structures but I get a "cannot convert 'struct' to 'struct*' error
当我尝试将此结构数组传递到函数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);
}
- 在C++中将结构转换和存储为二进制
- 如何大致将 cpp 代码结构转换为 python
- 如何将可变大小的结构转换为字符数组
- 尝试传递结构数组,但出现"无法将'结构'转换为'结构*'错误
- 我可以安全地将浮点数的结构转换为浮点数组吗C++?
- 如何将结构转换为字符串?
- 如何将C++结构转换为 C# 等效结构?
- 将结构转换为布尔值
- 将具有不同字段的结构转换为 uint8 的数组
- 将 constexpr 结构转换为运行时结构
- 将结构转换为类
- 我必须将记录结构转换为一类,并且不断遇到多个错误
- 如何将 ANSI C 结构转换为 C++ 类,但保持其 ANSI C 友好
- 将 C++ 中的结构转换为 Java 中的类
- 将结构转换为 constexpr 数组uint8_t
- 将大型结构转换为小型C
- c++正在将未知结构转换为字符串
- C++隐式将平凡可构造的结构转换为成员
- 将C++联合结构转换为 VB6
- C到C++:将结构转换为类