类定义字符串错误,求正确方法.

来源:百度知道 编辑:UC知道 时间:2024/06/04 02:30:40
#include <iostream>
#include <string.h>
using namespace std;
class CScore
{
private:
int No;
char Name[8];
float Math;
float Phi;
float sum;
public:
void Input(int no,char *name,float math,float phi);
void Sum();
void Show();
};
void CScore::Input(int no, char name, float math, float phi)
{
No=no;
strcpy(Name,name);
Math=math;
Phi=phi;
}
void CScore::Sum()
{
float s;
s=Math+Phi;
sum=s;
}
void CScore::Show()
{
cout<<"该学生的数学成绩为:"<<this->Math<<endl;
cout<<"该学生的物理成绩为:"<<this->Phi<<endl;
cout<<"该学生的总成绩为:"<<this->sum<<endl;
}

void main()
{
CScore a;
int no;
char name[8];
float math,phi;
cout<<"请输入学生的学号与姓名:"<<endl;
cin>>no;

你好,请你看仔细了,在类里面你是这么定义Input函数的:
void Input(int no,char *name,float math,float phi);

而下面就是编译出错的地方你是这么写的:
void CScore::Input(int no, char name, float math, float phi)

现在知道错哪了吧,对应参数都不匹配了。在下面的name前面加个“*”就可以啦,我试过了

void Input(int no,char *name,float math,float phi);

void CScore::Input(int no, char name, float math, float phi) //这里的实现和声明不一样,第二个参数应是char*