c++编程的问题
源代码#include<iostream.h>classCStuScore{public://共有类型声明charstrName[12];//姓名charstrStuNO...
源代码
#include<iostream.h>
class CStuScore
{
public: //共有类型声明
char strName[12]; //姓名
char strStuNO[9]; //学号
void SetScore(float s0,float s1,float s2) //成员函数:设置3门课程成绩
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private: //私有类型声明
float fScore[3]; //三门课程成绩
}; //注意分号不能省略
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore={"strName","strStuNO",{s0,s1,s2}};
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<"\n";
}
--------------------Configuration: asdvvv - Win32 Debug--------------------
Compiling...
asdvvv.cpp
F:\Program Files\Microsoft Visual Studio\MyProjects\asdvvv\asdvvv.cpp(21) : error C2513: 'class CStuScore' : no variable declared before '='
F:\Program Files\Microsoft Visual Studio\MyProjects\asdvvv\asdvvv.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
asdvvv.exe - 2 error(s), 0 warning(s)
总提示这些错误,怎么改啊?改对的有追加
怎么改是主要!!!!!! 展开
#include<iostream.h>
class CStuScore
{
public: //共有类型声明
char strName[12]; //姓名
char strStuNO[9]; //学号
void SetScore(float s0,float s1,float s2) //成员函数:设置3门课程成绩
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private: //私有类型声明
float fScore[3]; //三门课程成绩
}; //注意分号不能省略
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore={"strName","strStuNO",{s0,s1,s2}};
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<"\n";
}
--------------------Configuration: asdvvv - Win32 Debug--------------------
Compiling...
asdvvv.cpp
F:\Program Files\Microsoft Visual Studio\MyProjects\asdvvv\asdvvv.cpp(21) : error C2513: 'class CStuScore' : no variable declared before '='
F:\Program Files\Microsoft Visual Studio\MyProjects\asdvvv\asdvvv.cpp(21) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
asdvvv.exe - 2 error(s), 0 warning(s)
总提示这些错误,怎么改啊?改对的有追加
怎么改是主要!!!!!! 展开
6个回答
展开全部
你的问题很多啊:1、没有定义类对象。2、对象初始化错误(没有定义构造函数).3、字符串数组问题;
下面是我的两种改进方法:
一、(使用构造函数)
#include<iostream.h>
class CStuScore
{
public:
CStuScore(float a1,float a2,float a3,char *p)//定义构造函数,以初始化类对象;
{
strName=p;//姓名;
fScore[0]=a1;
fScore[1]=a2;
fScore[2]=a3;
}
char *strName; //用字符串指针比字符串数组要方便些;
char strStuNO[9];
void SetScore(float s0,float s1,float s2) //有了构造函数,这个函数有点多余;
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private:
float fScore[3];
};
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore ak=CStuScore(50,60,70,"小强");//定义类对象,并初始化;
cout<<ak.strName<<"strName的平均成绩为: "<<ak.GetAverage()<<"\n"; //输出结果;
}
二、(使用你自己定义的函数)
#include<iostream.h>
class CStuScore
{
public:
char *strName;
char strStuNO[9];
void SetScore(float s0,float s1,float s2)
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private:
float fScore[3];
};
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore ak;
ak.strName="小强";
ak.SetScore(100,80,80);
cout<<ak.strName<<"strName的平均成绩为: "<<ak.GetAverage()<<"\n";
}
建议使用第一种方法(也就是构造函数初始化方法),这才是类的真正精髓;
下面是我的两种改进方法:
一、(使用构造函数)
#include<iostream.h>
class CStuScore
{
public:
CStuScore(float a1,float a2,float a3,char *p)//定义构造函数,以初始化类对象;
{
strName=p;//姓名;
fScore[0]=a1;
fScore[1]=a2;
fScore[2]=a3;
}
char *strName; //用字符串指针比字符串数组要方便些;
char strStuNO[9];
void SetScore(float s0,float s1,float s2) //有了构造函数,这个函数有点多余;
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private:
float fScore[3];
};
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore ak=CStuScore(50,60,70,"小强");//定义类对象,并初始化;
cout<<ak.strName<<"strName的平均成绩为: "<<ak.GetAverage()<<"\n"; //输出结果;
}
二、(使用你自己定义的函数)
#include<iostream.h>
class CStuScore
{
public:
char *strName;
char strStuNO[9];
void SetScore(float s0,float s1,float s2)
{
fScore[0]=s0; fScore[1]=s1; fScore[2]=s2;
}
float GetAverage();
private:
float fScore[3];
};
float CStuScore::GetAverage()
{
return(float)((fScore[0]+fScore[1]+fScore[2])/3.0);
}
void main()
{
CStuScore ak;
ak.strName="小强";
ak.SetScore(100,80,80);
cout<<ak.strName<<"strName的平均成绩为: "<<ak.GetAverage()<<"\n";
}
建议使用第一种方法(也就是构造函数初始化方法),这才是类的真正精髓;
展开全部
第六个输出的就是负三,因为只有++和--才能改变a自身的数值,第五个虽然a前加个负号但是a仍然是2,然后a++成3,到第六步输出负三。谢谢采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
双击错误信息不就定位到错误代码处了吗???
这句明显错误
CStuScore={"strName","strStuNO",{s0,s1,s2}};
CStuScore是类,怎么能={....}
至少是CStuScore a = {....}
但你又没重载操作符= 所以也不能直接用=
这句明显错误
CStuScore={"strName","strStuNO",{s0,s1,s2}};
CStuScore是类,怎么能={....}
至少是CStuScore a = {....}
但你又没重载操作符= 所以也不能直接用=
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
最后几行
void main()
{
CStuScore={"strName","strStuNO",{s0,s1,s2}};
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<"\n";
}
改成
void main()
{
CStuScore t = {"strName","strStuNO",{s0,s1,s2}};
cout<<t.strName<<"strName的平均成绩为: "<<t.GetAverage()<<"\n";
}
void main()
{
CStuScore={"strName","strStuNO",{s0,s1,s2}};
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<"\n";
}
改成
void main()
{
CStuScore t = {"strName","strStuNO",{s0,s1,s2}};
cout<<t.strName<<"strName的平均成绩为: "<<t.GetAverage()<<"\n";
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
()StrName什么东东
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<endl;
cout<<()StrName<<"strName的平均成绩为: "<<GetAverage()<<endl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询