错误C2662:"Student :: finalGrade":无法将"this"指针从"const Student"转换为"Student&" [英] Error C2662: 'Student::finalGrade' : cannot convert 'this' pointer from 'const Student' to 'Student &'

查看:178
本文介绍了错误C2662:"Student :: finalGrade":无法将"this"指针从"const Student"转换为"Student&"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚该错误的解决方法.感谢任何人的帮助


Can''t figure out how to fix this error. Thanks for anyone''s help


#include <iostream>
using namespace std;

class Student

{
    public: Student();
            Student (int, int, int, int);
            double quizAverage();
            double finalGrade();
            void displayGrade()const;

    private: int id;
             int quiz1;
             int quiz2;
             int finalExam;

};

Student::Student()
{
    id= 0;
    quiz1= 40;
    quiz2= 40;
    finalExam= 40;

}

Student::Student(int ID ,int QUIZ1 ,int QUIZ2,int FINAL)
{
    id= ID;
    quiz1= QUIZ1;
    quiz2= QUIZ2;
    finalExam= FINAL;
}

double Student::quizAverage()
{
    double average;
    average = (quiz1 + quiz2)/2;
    return average;

}

double Student::finalGrade()
{
    double finalgr;
    finalgr = (quizAverage() + finalExam)/2;
    return finalgr;

}

void Student::displayGrade()const
{
    cout<<"The student's ID is:\t"<<id<<endl;
    cout<<"The final grade is:\t"<<finalGrade()<<endl;

}

推荐答案

您已经从emilio那里得到了这个问题的答案,但这只是给您的提示.

如果您的代码位于.h和.cpp文件中(在此处看不到),请不要在头文件中放置using语句.请记住,该语句将包含在所有文件(包括标题)中,这可能会很烦人.

另外,一个好主意是将形式参数的名称保留在声明(您的第二个构造函数)中,因为这是您的类中暴露给他人的部分.用户不必阅读实现即可了解如何使用方法.
You already have an answer to this question from emilio, and this is just a tip for you.

If you have your code in a .h and .cpp file, which is impossible to see here, don\''t put a using statement in your header file. Remember that the statement will be included in all files including your header, and that can be annoying.

Also, a good idea is to keep the names of the formal parameters in the declaration (your second constructor) since this is the part of your class that is exposed to others. Users should not have to read the implementation to understand how to use a method.


<code> dispalyGrade是const,但不是finalGrade .
const 函数不能调用非常量函数.

由于sinppet中的所有功能都是"访问器"(它们不需要修改类成员),因此全部为const.
<code>dispalyGrade is const, but finalGrade is not.
A const function cannot invoke non-const functions.

Since all the function in the sinppet are "accessor" (they don''t need to modify the class members) let all be const.


这篇关于错误C2662:"Student :: finalGrade":无法将"this"指针从"const Student"转换为"Student&amp;"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆