gpt4 book ai didi

使用结构变量时出现 C++ 错误

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:10:19 25 4
gpt4 key购买 nike

我有这个结构:

struct noduri {
int nod[100];
};

还有这个函数:

int clearMatrix(int var)
{
cout << all[1].nod[30];
}

int main()
{
noduri all[100];
cout << all[1].nod[30];
return 0;
}

我希望将结构分配给数组 all[] 的所有 100 个元素, 当我做 cout << all[1].nod[30];一切正常,没有错误,它输出 0 .当我调用 clearMatrix(1)我收到此错误:error: request for member nod in all[1], which is of non-class type int ,我做错了什么?!

最佳答案

数组变量 allmain 函数的局部变量,因此您不能在 clearMatrix 中引用它,除非您将指向它的指针传递给函数:

int clearMatrix(int var, noduri *all)
{
cout<<all[1].nod[30];
}


int main()
{
noduri all[100];
clearMatrix(5, all);
return 0;
}

关于使用结构变量时出现 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22730602/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com