我有来自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;
}
Run Code Online (Sandbox Code Playgroud)
将类型'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;
}
Run Code Online (Sandbox Code Playgroud)
我改变了比较函数的声明:
bool compare(const Student_info&, Student_info&);
bool compare(const Student_info, Student_info);
Run Code Online (Sandbox Code Playgroud)
现在它编译.
Dav*_*eas 34
该错误表明您不能将非const引用绑定到const对象,因为它会丢弃(丢弃其他编译器的错误),忽略或忽略const限定符.
它试图指出的是,如果允许操作,你将能够通过引用修改对象,忽略对象本身的事实const,破坏const正确性.
在您的特定代码中,__median库中的函数采用__a,__b并且__c通过const引用并尝试调用__comp函数,该函数在您的程序(第一个声明)中通过非const引用获取第二个参数.为了能够调用__comp(__a,__b)(或__comp在该函数中的任何其他调用),它必须将只能通过a访问的对象绑定const&到采用非const引用的第二个参数.这很可能是一个错字,因为你compare在下面定义了两个参数都是const引用.
将comparemain之前的声明更改为:
bool compare(const Student_info&, const Student_info&);
// ^^^^^
Run Code Online (Sandbox Code Playgroud)
也可以error: binding reference of type \xe2\x80\x98((type))&\xe2\x80\x99 to \xe2\x80\x98const ((type))\xe2\x80\x99 discards qualifiers源自类的const 成员函数,例如void MyClass::myFunc() const {}
#include <iostream>\n\n// comment next line to make code work\n#define BROKEN\n\nclass C2 {\npublic:\n void f(int &i);\n};\nvoid C2::f(int &i) { i++; }\n\nclass C1 {\npublic:\n C1();\n int i;\n C2 c2; C2* pc2;\n void f_nonconst();\n void f_const() const;\n};\nC1::C1() { c2 = C2(); pc2 = &c2; }\n\nvoid C1::f_nonconst() {\n pc2->f(i);\n // no error\n}\n\n#ifdef BROKEN\nvoid C1::f_const() const {\n pc2->f(i);\n // error: binding reference of type \xe2\x80\x98int&\xe2\x80\x99 to \xe2\x80\x98const int\xe2\x80\x99 discards qualifiers\n}\n#endif\n\n#define print_i() { std::cout << "i = " << pc1->i << std::endl; }\nint main() {\n C1 c1 = C1();\n C1* pc1 = &c1;\n\n print_i(); pc1->f_nonconst();\n#ifdef BROKEN\n print_i(); pc1->f_const();\n#endif\n print_i();\n}\nRun Code Online (Sandbox Code Playgroud)\n\n
| 归档时间: |
|
| 查看次数: |
51611 次 |
| 最近记录: |