c++帮帮忙printfstudentinto那里该怎么传参
#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
Student(string id="NULL",double score=0);
static double sum();
static double average();
string getid();
double getscore();
void setid(string s);
void setscore(double s);
void printfstudentinto();
private:
string id;
double score;
static double total_score;
static double count;
};
string Student::getid()
{
return id;
}
double Student::getscore()
{
return score;
}
void Student::printfstudentinto()
{
cout<<"学生信息: ";
cout<<"id: "<<getid()<<"score: "<<getscore();
}
void Student::setid(string s)
{
id=s;
}
void Student::setscore(double s)
{
score=s;
++count;
total_score=total_score+score;
}
double Student::sum()
{
return total_score;
}
double Student::average()
{
double average=0.0;
average=total_score/count;
return average;
}
double Student::total_score=0.0;
double Student::count=0.0;
int main()
{
Student stu[10];
int n;
double s;
string id;
cout<<"请输入学生的·人数(1~10):";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"请输入第 "<<i+1<<" 位学生的id、score: ";
cin>>id;
cin>>s;
stu[i].setid(id);
stu[i].setscore(s);
}
printfstudentinto(stu,n);
cout<<"total_score:"<<Student::sum()<<endl;
cout<<"average:"<<Student::average()<<endl;
return 0;
}
你的
printfstudentinto(stu,n);
调用完全不正确,它是Student成员函数,不能直接调用,且它没有参数
正确的方法,改为
for(int i=0; i<n; i++)
stu[i].printfstudentinto( );
另外,你的
Student(string id="NULL",double score=0);
虽然有缺省参数,但没有实现,所以要改为
Student(string id="NULL",double score=0) {}
这样程序可以运行,其它小问题你自己再看下
上一篇:vb软件提示这个如何解决,谢谢!