我收到了一个我从未遇到过的错误。很可能它的东西非常简单,因为我一直在拉毛发找到这个小错误。所以,我决定把它带给专业人士。
我想在这里完成的任务:我需要搜索我的医生矢量并获得身份证。
编译时得到的错误消息:
main.cpp:46:43: error: invalid initialization of non-const reference of type ‘std::vector<Doctor>&’ from an rvalue of type ‘int’
main.cpp:22:16: error: in passing argument 1 of ‘DoctorIterator DocSearch(std::vector<Doctor>&, int)’
我知道我的线性搜索功能是正确的,因为我有一个类似的,但是当我调用searhc函数时,我得到了如上所述的那些错误。
这是线性搜索功能
//Linear search through the doctor vector
typedef vector<Doctor>::iterator DoctorIterator;
DoctorIterator DocSearch(vector<Doctor> &doctors, int id) {
DoctorIterator it = doctors.begin();
for ( ; it < doctors.end() and id != it->getId(); it++) {
return it;
}
}
打印帐单功能的算法
print a bill for (p) { // p is a patient
total = 0
print the header information for the bill
for each entry b in the vector of billings
if b's person-id matches p's person-id
doctor = search for b's id in the vector of doctors
print the billing line (date, treatment, doctor, charge)
add charge to total print the total line
}
以下是调用线性搜索功能的函数
//Prints the bill for each patient
void PrintABillForP(Billing b, Patient p, Doctor d, vector<Billing> billing, vector<Doctor> doctors) {
int total = 0;
cout << "Patient: " << ' ' << p.getName() << ' ' << "Id: " << p.getId() << ' ' << "Ailment: " << ' ' << p.getAilment() << endl;//Need to adjust colum widths
cout << endl;
cout << "Treatment Date " << ' ' << "Treatment " << ' ' << "Doctor " << ' ' << "Charge" << endl;
for (auto i : billing) {
if (b.getPatientID() == p.getId()) {
DocSearch(doctors, b.getPatientID);
}
cout << b.getDateOfTreatment() << ' ' << d.getSpecialty() << ' ' << d.getName() << ' ' << d.getRate() << endl;
cout << endl << endl;
total = total + d.getRate();
}
cout << "Total: " << ' ' << '$' << total;
}
答案 0 :(得分:1)
it < doctors.end()
应为it != doctors.end()
。return it;
应该在循环之外。d.getId()
按值而不是按引用返回vector
。答案 1 :(得分:0)
错误消息显示:
main.cpp:46:43: error: invalid initialization of non-const reference of type
‘std::vector<Doctor>&’ from an rvalue of type ‘int’
我认为d.getId()
正在返回int
,这不是DocSearch所期望的。
此外,您可能不希望按值传递对象,例如
void PrintABillForP(Billing b, Patient p, Doctor d, vector<Billing> billing)
不清楚Billing
,Patient
和Doctor
类型是什么,但按值vector
传递将导致副本生成,这不太可能是你想要的。