Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 可以在这两个函数中设置类函数吗?_C++_Function_Class_Static Members - Fatal编程技术网

C++ 可以在这两个函数中设置类函数吗?

C++ 可以在这两个函数中设置类函数吗?,c++,function,class,static-members,C++,Function,Class,Static Members,我不知道如何将我的类函数调用到printData(Testscore&)和readData(Testscore)中 另外,有人能告诉我为什么我的Average()没有被调用到main?我刚刚学习了如何使用静态成员变量和静态成员函数,我想知道我是否错误地使用了它们 readData功能: 不使用复制构造函数 读取所有实例变量,如学生的姓名和所有 等级 使用函数存储指向privatepquiz的每个级别数组的变量、名称、元素以及静态成员 等级要读取的等级数 printData功能: 写下测验的名称和

我不知道如何将我的类函数调用到
printData(Testscore&)
readData(Testscore)

另外,有人能告诉我为什么我的
Average()
没有被调用到
main
?我刚刚学习了如何使用静态成员变量和静态成员函数,我想知道我是否错误地使用了它们

readData
功能:

  • 不使用复制构造函数
  • 读取所有实例变量,如学生的姓名和所有 等级
  • 使用函数存储指向private
    pquiz
    的每个级别数组的变量、名称、元素以及静态成员
    等级
    要读取的等级数
  • printData
    功能:

  • 写下测验的名称和平均成绩
  • 使用复制构造函数
  • 这是我目前的计划:

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class TestScore {
    private:
        static int grades;
        string name;
        double *pquiz;
        double average;
    public:
        TestScore();
        ~TestScore();
        void setName(string);
        static void setGrades(int);
        void setPquiz(double *);
        void setAverage(double);
        string getName();
        static int getGrades();
        double getPquiz();
        void readData(TestScore &);
        void printData(TestScore);
        double Average(double *, int);
        static void Grade(int);
    };
    
    TestScore::TestScore() {
        name="";
        pquiz=new double[grades];
        average=0;
    }
    
    void TestScore::setName(string name1) {
        if(name1!="1") {
            name=name1;
        }
    }
    
    void TestScore::setPquiz(double *pquiz1) {
        if(pquiz>=0) {
            pquiz=pquiz1;
        }
    }
    
    void TestScore::setGrades(int grades1) {
        if(grades1>=0) {
            grades=grades1;
        }
    }
    
    void TestScore::setAverage(double average1) {
        if(average1>=0) {
            average=average1;
        }
    }
    
    string TestScore::getName() {
        return name;
    }
    
    int TestScore::getGrades() {
        return grades;
    }
    
    double TestScore::getPquiz() {
        return *pquiz;
    }
    
    
    double Average(double *pquiz, int grade) {
        int count;
        double total=0;
        double average=0;
        for(count=0; count<grade; count++) {
            total+=pquiz[count];
        }
        average=total/grade;
        return average;
    }
    
    void readData(TestScore&) {
    
    }
    void printData(TestScore) {
    
    }
    
    TestScore::~TestScore() {
        delete [] pquiz;
        pquiz=0;
    }
    
    int TestScore::grades=0;
    void TestScore::Grade(int a) {
        grades+=a;
    }
    
    
    
    int main() {
        const int grades = 3;
        const int students = 4;
        TestScore exam;
        string student;
        int grade;
        double *pquiz;
        double average;
        for(int i=0; i<students; i++) {
            cout<<"Student "<<(i+1)<<": ";
            cin>>student;
            exam.setName(student);
            cout<<endl;
    
            for(int count=0; count<grades; count++) {
                cout<<"Quiz "<<(count+1)<<": ";
                cin>>pquiz[count];
                exam.setPquiz(pquiz);
                exam.getPquiz();
    
                while(pquiz[count]<0) {
                    cout<<"Error, invalid test score, please try again."<<endl;
                    cout<<"Quiz "<<(count+1)<<": ";
                    cin>>pquiz[count];
                }
            }
            exam.setAverage(average);
            cout<<exam.getName()<<" average is "<<Average(pquiz, grade)<<endl<<endl;
        }
        readData(exam);
        printData(exam);
    
        return 0;
    }
    
    #包括
    #包括
    使用名称空间std;
    班级考试成绩{
    私人:
    静态智力等级;
    字符串名;
    双*pquiz;
    双倍平均;
    公众:
    TestScore();
    ~TestScore();
    void setName(字符串);
    静态空隙率(int);
    无效setPquiz(双*);
    平均值(双倍);
    字符串getName();
    静态int getGrades();
    双getPquiz();
    无效读取数据(TestScore&);
    无效打印数据(TestScore);
    双倍平均(双倍*,整数);
    静态空隙度(int);
    };
    TestScore::TestScore(){
    name=“”;
    pquiz=新的双[等级];
    平均值=0;
    }
    void TestScore::setName(字符串名称1){
    如果(名称1!=“1”){
    name=name1;
    }
    }
    无效测试分数::setPquiz(双*pquiz1){
    如果(pquiz>=0){
    pquiz=pquiz1;
    }
    }
    void TestScore::setGrades(int grades1){
    如果(等级1>=0){
    年级=1年级;
    }
    }
    void TestScore::setAverage(双平均值1){
    如果(平均值1>=0){
    平均值=平均值1;
    }
    }
    字符串TestScore::getName(){
    返回名称;
    }
    int TestScore::getGrades(){
    返回等级;
    }
    双重测试分数::getPquiz(){
    返回*pquiz;
    }
    双倍平均(双倍*pquiz,整数等级){
    整数计数;
    双倍合计=0;
    双平均=0;
    
    对于(count=0;count不要在任何地方使用
    static
    ,至少现在不要。您有太多同名变量,分散在各地。请尝试清理它们

    TestScore::TestScore()
    {
        name = "";
        //pquiz = new double[grades];//#grades is undefined
        pquiz = NULL;
        average = 0;
    }
    
    grades
    尚未定义,它可以是零,也可以是-817。您应该删除该行,或者您可以添加类似于
    pquiz=new double[10]
    的内容,前提是您确定测验数量不会超过10

    TestScore::~TestScore()
    {
        if (pquiz) delete[] pquiz;
        pquiz = NULL;
    }
    
    delete
    pquiz
    仅当它不是
    NULL

    int main() {
        const int grades = 3;
        const int students = 4;
        TestScore exam;
        string student;
        int grade;
        double *pquiz;
    ...
    

    这是一个不同的
    pquiz
    ,它是一个不指向任何东西的指针,它并不存在,你不能这样使用它。

    grades
    不应该是静态的。
    delete pquiz
    只有当它不是
    NULL
    delete NULL;
    很好,它会变成一个nop。为什么我不能在主菜单中声明指针还是帮不了我的问题