Overloading_grade_program_accelerated_c++_本书

Overloading_grade_program_accelerated_c++_book

所以我正在做一本“Accelerated C++”书中的练习。但是,当我尝试修改书中的代码时,Visual Studio 一直在说不止一个重载函数实例 grade 与参数列表相匹配。但我看不出超载问题在哪里。有人可以帮我解决这个问题吗?

grade.h

#ifndef GUARD_grade_h
#define GUARD_grade_h

#include <vector>
#include "student_info.h"

double grade(double, double, double);
double grade(double, double, const std::vector<double>&);
double grade(const Student_info &);

#endif // !GUARD_grade_h

grade.cpp

#include <vector>
#include <stdexcept>
#include "median.h"
#include "student_info.h"
#include "grade.h"

using std::domain_error; using std::vector;

double grade(double midterm, double final, double homeworks) 
{
    return 0.2 * midterm + 0.4 * final + 0.4 * homeworks;
}

double grade(double midterm, double final, const vector<double> hm)
{
    if (hm.size() == 0)
        throw domain_error("student has done no homework");
    return grade(midterm, final, median(hm));
}

double grade(const Student_info& s)
{
    return grade(s.midterm, s.final, s.homework); // this grade is saying that is more then one
    //instance of overloaded function "grade"
}

grade() 的第二个重载(以向量作为第三个参数的重载)的声明和定义不匹配。声明通过引用传递向量,但您的定义通过值传递向量。您在后者中漏掉了一个 &

就目前而言,当编译器看到 grade(s.midterm, s.final, s.homework); 调用(在最后一个重载的定义中)时,它不知道要使用两个 'possible' 重载中的哪一个:传递 s.homework 按引用还是按值?