vc6.0中出现的问题:不能访问类的私有变量
student.h程序#ifndef _STUDENT_H_
#define _STUDENT_H_
#include <string>
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 returnTotalPreformance();
double returnAverage();
~CStudent(){}
};
#endif
主程序
#include <iostream>
#include "student.h"
using namespace std;
int main()
{
CStudent student1("XiaoMing",50,80,98);
CStudent student2("XiaoHua",70,78,68);
CStudent student3("NingJian",86,78,83);
// 1、读取某同学单科分数
cout<<student1<<endl;
cout<<student2<<endl;
cout<<student3<<endl;
// 2、重新设置分数
student1.SetChinese(56);
student2.SetEnglish(87);
student3.SetMath(87);
// 3、计算不同学生同学科的总成绩
CStudent General ;
General = student1+student2+student3;
cout<<General;
// 4、查看多个人的各科目平均成绩
cout<<"Average of Chinese :"<<General.returnChinese()/3
<<"\nAverage of Math :"<<General.returnMath()/3
<<"\nAverage of English :"<<General.returnEnglish()/3<<endl;
// 5、查看个人平均成绩
cout<<"Average of students : \n";
cout<<student1.returnName()<<" : "<<student1.returnAverage()<<endl;
cout<<student2.returnName()<<" : "<<student2.returnAverage()<<endl;
cout<<student3.returnName()<<" : "<<student3.returnAverage()<<endl;
// 6、查看个人总成绩
cout<<"Total of students : \n";
cout<<student1.returnName()<<" : "<<student1.returnTotalPreformance()<<endl;
cout<<student2.returnName()<<" : "<<student2.returnTotalPreformance()<<endl;
cout<<student3.returnName()<<" : "<<student3.returnTotalPreformance()<<endl;
cin.get();
return 0 ;
}
头文件中student.cpp程序
#include <iostream>
#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 Preformance";
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::returnTotalPreformance()
{
return chinese + math + english ;
}
double CStudent::returnAverage()
{
return (chinese + math + english)/3;
}
string CStudent::returnName()
{
return strName;
}
程序运行后出现错误
D:\网上下载的vc++(1月29日)\MSDev98\MyProjects\重复\student.cpp(29) : error C2248: 'strName' : cannot access private member declared in class 'CStudent'
d:\网上下载的vc++(1月29日)\msdev98\myprojects\重复\student.h(8) : see declaration of 'strName'
D:\网上下载的vc++(1月29日)\MSDev98\MyProjects\重复\student.cpp(29) : error C2248: 'chinese' : cannot access private member declared in class 'CStudent'
d:\网上下载的vc++(1月29日)\msdev98\myprojects\重复\student.h(9) : see declaration of 'chinese'
D:\网上下载的vc++(1月29日)\MSDev98\MyProjects\重复\student.cpp(29) : error C2248: 'math' : cannot access private member declared in class 'CStudent'
d:\网上下载的vc++(1月29日)\msdev98\myprojects\重复\student.h(10) : see declaration of 'math'
D:\网上下载的vc++(1月29日)\MSDev98\MyProjects\重复\student.cpp(29) : error C2248: 'english' : cannot access private member declared in class 'CStudent'
d:\网上下载的vc++(1月29日)\msdev98\myprojects\重复\student.h(11) : see declaration of 'english'
Error executing cl.exe.
为什么会出现不能访问类的私有变量呢?
我是初学者,照书上写的程序,可运行不出来,希望各位高手帮忙,谢啦