询问一个关于结构体的问题

rock178 2009-12-13 11:22:14
小弟刚刚学习C++做题的时候遇到个问题,还请老师指导下:
#include<iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score[3];
};
void print(student stu[5]);
void input(student stu[5]);
int main()
{
student stu[5];
input(stu[5]); //这一行报错
print(stu[5]); //这一行报错
return 0;
}

报错如下:
error C2664: 'input' : cannot convert parameter 1 from 'struct student' to 'struct student []'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

为什么要"convert parameter 1 from 'struct student' to 'struct student []'"呢?声明的时候参数就是结构体的数组,实参给的也是结构体的数组啊?
...全文
201 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
peijicheng 2010-07-14
  • 打赏
  • 举报
回复
数组名做参数传递的是地址!!……
zhenghui2006 2010-05-25
  • 打赏
  • 举报
回复
没有stu[5]这个数据,因为是从0开始的,只能到4,所以改成stu[4] 就没有问题了!
tanleihexinxin 2009-12-13
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score[3];
};
void print(student stu[5])
{

}
void input(student stu[5])
{

}
int main()
{
student stu[5];
input(stu); //这一行报错
print(stu); //这一行报错
return 0;
}
lirg8405 2009-12-13
  • 打赏
  • 举报
回复
void print(student stu[5]);
void input(student stu[5]);
上面的5害你,其实应该去掉里面的5
student stu[5]; //这里声明的只是一个包含5个结构体的数组,下标从0到4
input(stu[5]); //这一行报错
print(stu[5]); //这一行报错
所有上面第一个错误是,根本取不到stu[5],函数里面的stu[5]是取数组的第5个元素,这是根本不存在的
第二个错误就是上面的就,直接传stu进去
wanjiwz 2009-12-13
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 gueangyik 的回复:]
#include <iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score[3];
};
void print(student stu[5]);
void input(student stu[5]);
int main()
{
student stu[5];
input(stu);  //这一行报错
print(stu);  //这一行报错
return 0;
}
刚才那个上面的错了
[/Quote]
这两个函数没有实现啊,编译是可以通过的,链接就不行了
gueangyik 2009-12-13
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score[3];
};
void print(student stu[5]);
void input(student stu[5]);
int main()
{
student stu[5];
input(stu); //这一行报错
print(stu); //这一行报错
return 0;
}
刚才那个上面的错了
gueangyik 2009-12-13
  • 打赏
  • 举报
回复

#include <iostream>
using namespace std;
struct student
{
int num;
char name[10];
float score[3];
};
void print(student stu);
void input(student stu);
int main()
{
student stu[5];
input(stu[5]); //这一行报错
print(stu[5]); //这一行报错
return 0;
}

liao05050075 2009-12-13
  • 打赏
  • 举报
回复
input(stu[5]);
改成input(stu),只要数组名就行,不用[5]
stardust20 2009-12-13
  • 打赏
  • 举报
回复
input(stu); //这一行报错
print(stu); //这一行报错
zyx124455 2009-12-13
  • 打赏
  • 举报
回复
input(stu[5]); 改成input(stu);
bhc29 2009-12-13
  • 打赏
  • 举报
回复
input(stu[5]); //这一行报错
print(stu[5]); //这一行报错

而且最大只能是stu[4]....
bhc29 2009-12-13
  • 打赏
  • 举报
回复
void print(student stu[5]);
void input(student stu[5]);

申明是传入数组,其实就是就是指向数组的头指针,即&stu[0],写为stu.


input(stu[5]); //这一行报错
print(stu[5]); //这一行报错

而你却传入了数据本身,而不是指针,所以报这样的错:
convert parameter 1 from 'struct student' to 'struct student []'
即申明的数据类型和传入的数据类型不相符!

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧