引用的绑定类型为drop qualifiers的值

时间:2015-01-07 03:47:23

标签: c++

我有来自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;
}

将引用绑定到类型&#39; Student_info&#39;类型为&#39; const Student_info&#39; drop qualifiers 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);

现在它编译。<​​/ p>

2 个答案:

答案 0 :(得分:27)

该错误表明您无法将非const引用绑定到const对象,因为 drop discard 在其他编译器的错误中) ),忽略或忽略const限定符。

它试图指出的是,如果允许操作,你将能够通过引用修改对象,忽略对象本身为const的事实,打破了const-correctness。

在您的特定代码中,库中的函数__median通过const引用获取__a__b__c并尝试调用__comp函数,在你的程序中(第一个声明)通过非const引用获取第二个参数。为了能够在该函数中调用__comp(__a,__b)(或对__comp的任何其他调用),它必须将只能通过const&访问的对象绑定到采用非compare的第二个参数const参考。这很可能是一个错字,因为你定义下面的compare,两个参数都是const引用。

将main之前的bool compare(const Student_info&, const Student_info&); // ^^^^^ 声明更改为:

{{1}}

答案 1 :(得分:1)

error: binding reference of type ‘((type))&’ to ‘const ((type))’ discards qualifiers也可以源自类的 const成员函数,例如void MyClass::myFunc() const {}

#include <iostream>

// comment next line to make code work
#define BROKEN

class C2 {
public:
    void f(int &i);
};
void C2::f(int &i) { i++; }

class C1 {
public:
    C1();
    int i;
    C2 c2; C2* pc2;
    void f_nonconst();
    void f_const() const;
};
C1::C1() { c2 = C2(); pc2 = &c2; }

void C1::f_nonconst() {
    pc2->f(i);
    // no error
}

#ifdef BROKEN
void C1::f_const() const {
    pc2->f(i);
    // error: binding reference of type ‘int&’ to ‘const int’ discards qualifiers
}
#endif

#define print_i() { std::cout << "i = " << pc1->i << std::endl; }
int main() {
    C1 c1 = C1();
    C1* pc1 = &c1;

    print_i(); pc1->f_nonconst();
#ifdef BROKEN
    print_i(); pc1->f_const();
#endif
    print_i();
}

Meaning of 'const' last in a function declaration of a class?

有关