qq_44653106
Clanva
采纳率100%
2020-06-07 11:38

大神帮我运行一下c++,是不是我的软件有问题。。

已采纳

student.h内的内容:
#ifndef STUDENT_H
#define STUDENT_H
#include
using namespace std;
class CStudent
{
private:
string strName;
double chinese;
double math;
double english;
public:
CStudent();
CStudent(string Name , double c, double m , double e);
CStudent operator+(CStudent S);
friend ostream& operator<<(ostream& out , CStudent S);
void SetChinese(double a);
void SetMath(double a);
void SetEnglish(double a);
string returnName();
double returnChinese();
double returnMath();
double returnEnglish();
double returnTotalPerformance();
double returnAverage();
~CStudent(){}
};
#endif


student.cpp内的内容:
#include
#include"student.h"
using namespace std;
CStudent::CStudent()
{
strName = "General Student";
chinese = 0;
math = 0;
english = 0;
}
CStudent::CStudent(std::string Name, double c, double m,double e)
{
strName = Name ;
chinese = c ;
math = m ;
english = e ;
}
CStudent CStudent::operator +(CStudent S)
{
CStudent temp;
temp.strName = "Total Performance";
temp.chinese = this->chinese +S.chinese;
temp.math = this->math + S.math;
temp.english = this->english + S.english;
return temp;
}
ostream& operator <<(ostream& out , CStudent S)
{
out<<S.strName<<"( "<<S.chinese<<" , "<<S.math<<" , "<<S.english<<" )"<<endl;
return out;
}
void CStudent::SetChinese(double a)
{
chinese = a;
}
void CStudent::SetEnglish(double a)
{
english = a;
}
void CStudent::SetMath(double a)
{
math = a;
}
double CStudent::returnChinese()
{
return chinese;
}
double CStudent::returnEnglish()
{
return english;
}
double CStudent::returnMath()
{
return math;
}
double CStudent::returnTotalPerformance()
{
return chinese+english+math;
}
double CStudent::returnAverage()
{
return (chinese+english+math) /3;
}
string CStudent::returnName()
{
return strName;
}


main.cpp内的内容:
#include
#include"student.h"
using namespace std;
int main()
{
CStudent student1("Xiao Ming", 50,80,98);
CStudent student2("Xiao Hua", 70,78,68);
CStudent student3("Ning Jian", 86,78,83);
cout<<student1<<endl;
cout<<student2<<endl;
cout<<student3<<endl;
student1.SetChinese(56);
student2.SetEnglish(87);
student3.SetMath(87);
CStudent General ;
General = student1 + student2 +student3;
cout<<"Average of Chinese : "<<General.returnChinese()/3
<<"\nAverage of Math : "<<General.returnMath()/3
<<"\nAverage of English : "<<General.returnEnglish()/3<<endl;
cout<<"Average of students : \n";
cout<<student1.returnName()<<" : "<<student1.returnAverage()<<endl;
cout<<student2.returnName()<<" : "<<student2.returnAverage()<<endl;
cout<<student3.returnName()<<" : "<<student3.returnAverage()<<endl;
cout<<"Total of students : \n";
cout<<student1.returnName()<<" : "<<student1.returnTotalPerformance()<<endl;
cout<<student2.returnName()<<" : "<<student2.returnTotalPerformance()<<endl;
cout<<student3.returnName()<<" : "<<student3.returnTotalPerformance()<<endl;
cin.get();
return 0;
}

运行后出现错误:
D:\Microsoft Visual Studio\MyProjects\Performance4\student.cpp(29) : error C2248: 'strName' : cannot access private member declared in class 'CStudent'
d:\microsoft visual studio\myprojects\performance4\student.h(8) : see declaration of 'strName'
D:\Microsoft Visual Studio\MyProjects\Performance4\student.cpp(29) : error C2248: 'chinese' : cannot access private member declared in class 'CStudent'
d:\microsoft visual studio\myprojects\performance4\student.h(9) : see declaration of 'chinese'
D:\Microsoft Visual Studio\MyProjects\Performance4\student.cpp(29) : error C2248: 'math' : cannot access private member declared in class 'CStudent'
d:\microsoft visual studio\myprojects\performance4\student.h(10) : see declaration of 'math'
D:\Microsoft Visual Studio\MyProjects\Performance4\student.cpp(29) : error C2248: 'english' : cannot access private member declared in class 'CStudent'
d:\microsoft visual studio\myprojects\performance4\student.h(11) : see declaration of 'english'

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • caozhy 从今以后生命中的每一秒都属于我爱的人 1年前

    ostream& operator <<(ostream& out , CStudent S)
    这个不是成员函数,不能访问private,你可以在CStudent里面定义为友元:
    在CStudent里面加上
    friend ostream& operator <<(ostream& out , CStudent S);

    点赞 1 评论 复制链接分享

相关推荐