2015-01-07 65 views
9

我从Text Accelerated C++获得以下源代码。当我尝试编译源文件时,出现下面列出的编译错误。我是C++语言的新手,所以你的帮助将不胜感激。绑定引用的类型值的限定符

#include <algorithm> 
#include <iomanip> 
#include <ios> 
#include <iostream> 
#include <string> 
#include <vector> 
#include <stdexcept> 

using namespace std; 

struct Student_info { 
    string name; 
    double midterm, final; 
    vector<double> homework; 
}; 

double median(vector<double>); 
double grade(const Student_info&); 
double grade(double, double, double); 
double grade(double, double, const vector<double>&); 
istream& read_hw(istream&, vector<double>&); 
istream& read(istream&, Student_info&); 
bool compare(const Student_info&, Student_info&); 

int main() { 

    vector<Student_info> students; 
    Student_info record; 
    string::size_type maxlen = 0; 

    while(read(cin, record)) { 
     maxlen = max(maxlen, record.name.size()); 
     students.push_back(record); 
    } 

    sort(students.begin(), students.end(), compare); 

    for(vector<Student_info>::size_type i = 0; 
      i != students.size(); ++i) { 

     cout << students[i].name << string(maxlen + 1 - students[i].name.size(), ' '); 

     try { 
      double final_grade = grade(students[i]); 
      streamsize prec = cout.precision(); 
      cout << setprecision(3) << final_grade 
        << setprecision(prec); 
     } catch(domain_error& e) { 
      cout << e.what(); 
     } 
     cout << endl; 
    } 
    return 0; 
} 

double median(vector<double> vec) { 
    typedef vector<double>::size_type vec_sz; 
    vec_sz size = vec.size(); 

    if(size == 0) 
     throw domain_error("median of an empty vector"); 

    sort(vec.begin(), vec.end()); 
    vec_sz mid = size/2; 

    return size%2 == 0 ? (vec[mid] + vec[mid - 1])/2 : vec[mid]; 
} 
double grade(const Student_info& s) { 
    return grade(s.midterm, s.final, s.homework); 
} 
double grade(double midterm, double final, double homework) { 
    return 0.2*midterm + 0.4*final + 0.4*homework; 
} 
double grade(double midterm, double final, const vector<double>& hw) { 
    if(hw.size() == 0) 
     throw domain_error("student has done no homework"); 
    return grade(midterm, final, median(hw)); 
} 
istream& read_hw(istream& in, vector<double>& hw) { 
    if(in) { 
     hw.clear(); 

     double x; 
     while(in >> x) 
      hw.push_back(x); 
     in.clear(); 
    } 

    return in; 
} 
istream& read(istream& is, Student_info& s) { 
    is >> s.name >> s.midterm >> s.final; 
    read_hw(is, s.homework); 
    return is; 
} 
bool compare(const Student_info& x, const Student_info& y) { 
    return x.name < y.name; 
} 

的参照结合到类型 'Student_info' 到类型 'const的Student_info' 的值下降限定符compute_grades_rev-B
线125,外部位置:/usr/include/c++/4.2.1/bits/ stl_algo.h C/C++问题

下面是从stl_algo.h的代码:

template<typename _Tp, typename _Compare> 
    inline const _Tp& 
    __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp) 
    { 
     // concept requirements 
     __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>) 
     if (__comp(__a, __b)) 
    if (__comp(__b, __c)) 
     return __b; 
    else if (__comp(__a, __c)) 
     return __c; 
    else 
     return __a; 
     else if (__comp(__a, __c)) 
    return __a; 
     else if (__comp(__b, __c)) 
    return __c; 
     else 
    return __b; 
    } 

我改变比较函数的声明从:

bool compare(const Student_info&, Student_info&); 
bool compare(const Student_info, Student_info); 

现在编译。

+0

这是行125? – user3553031

+0

@ user3553031 if(__comp(__ a,__b)) – dcrearer

回答

16

该错误是表示可以不结合非const引用到一个const对象,因为这将下降丢弃在其他编译器的错误),无视或忽略const限定符。

它试图说明的是,如果操作被允许,您将能够通过引用修改对象,忽略对象本身为const的事实,从而破坏const正确性。

在您的特定代码,在库中的功能__median通过const引用采取__a__b,和__c并试图调用__comp函数,它在程序中(第一个声明)由非const引用取第二个参数。为了能够调用__comp(__a,__b)(或在该函数中调用__comp),它必须将只能通过const&访问的对象绑定到接受非const引用的第二个参数。这很可能是一个错字,因为您在下面定义了compare,两个参数都是常量引用。

变化compare声明主要出现以下情况:

bool compare(const Student_info&, const Student_info&); 
//        ^^^^^ 
+0

你能解释一下你的位置吗?请从我的示例中更正一段代码请 – dcrearer

+0

真棒谢谢... – dcrearer