
立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,然后输出最大学生的学号和成绩
#include<iostream>usingnamespacestd;classxuesheng{private:intxuehao;intchengji;public...
#include <iostream>
using namespace std;
class xuesheng
{
private:
int xuehao;
int chengji;
public:
void display();
void max();
xuesheng(int a,int b)
{
xuehao=a;
chengji=b;
}
};
void xuesheng::max(xuesheng *p)
{
int i;
int max_g=0;
int max_i=0;
for(i=0;i<5;i++)
{
if((P+i)->chengji>max_g)
{
max_g=(p+i)->chengji;
max_i=i;
}
}
(P+max_i)->display();
}
void xuesheng::display()
{
cout<<"最高分的学生学号"<<xuehao<<endl;
cout<<"最高分的学生成绩"<<chengji<<endl;
}
void main()
{
xuesheng stud[5]={
xuesheng(1,98),
xuesheng(2,100),
xuesheng(3,97),
xuesheng(4,93),
xuesheng(5,92)
};
max();
}
错误信息
D:\C语言学习\12_1\12_1.cpp(19) : error C2511: 'max' : overloaded member function 'void (class xuesheng *)' not found in 'xuesheng'
D:\C语言学习\12_1\12_1.cpp(4) : see declaration of 'xuesheng'
D:\C语言学习\12_1\12_1.cpp(47) : error C2065: 'max' : undeclared identifier 展开
using namespace std;
class xuesheng
{
private:
int xuehao;
int chengji;
public:
void display();
void max();
xuesheng(int a,int b)
{
xuehao=a;
chengji=b;
}
};
void xuesheng::max(xuesheng *p)
{
int i;
int max_g=0;
int max_i=0;
for(i=0;i<5;i++)
{
if((P+i)->chengji>max_g)
{
max_g=(p+i)->chengji;
max_i=i;
}
}
(P+max_i)->display();
}
void xuesheng::display()
{
cout<<"最高分的学生学号"<<xuehao<<endl;
cout<<"最高分的学生成绩"<<chengji<<endl;
}
void main()
{
xuesheng stud[5]={
xuesheng(1,98),
xuesheng(2,100),
xuesheng(3,97),
xuesheng(4,93),
xuesheng(5,92)
};
max();
}
错误信息
D:\C语言学习\12_1\12_1.cpp(19) : error C2511: 'max' : overloaded member function 'void (class xuesheng *)' not found in 'xuesheng'
D:\C语言学习\12_1\12_1.cpp(4) : see declaration of 'xuesheng'
D:\C语言学习\12_1\12_1.cpp(47) : error C2065: 'max' : undeclared identifier 展开
3个回答
展开全部
max()函数是类的成员函数,不能直接调用 ,
而且声明时max没有参数,实现和调用时给了参数
而且声明时max没有参数,实现和调用时给了参数
追问
那怎么改呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
xuesheng(int a,int b)
{
xuehao=a;
chengji=b;
}
怎么没有返回值,就算没有也应该是void.
{
xuehao=a;
chengji=b;
}
怎么没有返回值,就算没有也应该是void.
更多追问追答
追问
没有这个 我下面的显示不了、我很不明下面max的error
追答
D:\C语言学习\12_1\12_1.cpp(19) : error C2511: 'max' : overloaded member function 'void (class xuesheng *)' not found in 'xuesheng'
这个函数void xuesheng::max(xuesheng *p)
没有声明
同时max();函数也没有函数体
建议在头文件中声明void xuesheng::max(xuesheng *p)
同时定义void max()函数的函数体。还有纠正一下,前面说没有返回值的事,你的方法是对的
xuesheng(int a,int b)
{
xuehao=a;
chengji=b;
}
的声明是正确的,没注意看是构造函数。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议你把VOID函数改成int,然后返回一个int
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询