我没有找任何人为我编码。但是,在调试修复代码时,这个概念正在逃避。
我知道我希望添加一些包含operator(n)
的代码,其中(n)
是运算符,但我不确定如何识别内容和位置。
尝试编译以下内容,我收到错误:
C2678 binary '<': no operator found which takes a left-hand operand of type 'Course'
C2678 binary '<': no operator found which takes a left-hand operand of type 'const Course'
C2678 binary '==': no operator found which takes a left-hand operand of type 'const Course'
编辑:添加.h,其中包含错误指向的代码:
vectorUtils.h(不是全部): 第一次错误:
template <typename T>
int addInOrder (std::vector<T>& vectr, T value)
{
// Make room for the insertion
int toBeMoved = vectr.size() - 1;
vectr.push_back(value);
// Shift elements up to make room
while (toBeMoved >= 0 && value < vectr[toBeMoved]) {
vectr[toBeMoved+1] = vectr[toBeMoved];
--toBeMoved;
}
// Insert the new value
vectr[toBeMoved+1] = value;
return toBeMoved+1;
}
第二&amp;第三个错误:
template <typename T>
int seqOrderedSearch(const std::vector<T> list, T searchItem)
{
int loc = 0;
while (loc < list.size() && list[loc] < searchItem)
{
++loc;
}
if (loc < list.size() && list[loc] == searchItem)
return loc;
else
return -1;
}
因为'<'
错误,我知道我正在寻找operator<
和'=='
我正在寻找operator==
。但同样,我不知道如何识别位置,更不用说在course.h或course.cpp中实现了。
再次,我不希望你修复它(如果你这样做,好吧,但我宁愿学习!)。
main.cpp中的函数:
void readCourses (istream& courseFile, vector<Course>& courses) {
while (courseFile) {
string courseName;
int courseMaxSize;
if (courseFile >> courseName >> courseMaxSize) {
Course c (courseName, courseMaxSize);
addInOrder (courses, c);
}
}
}
course.h:
#ifndef COURSE_H
#define COURSE_H
#include <iostream>
#include <string>
struct Course {
std::string name;
int enrollment;
int maxEnrollment;
std::string* students; // array of student names
Course (std::string courseName, int maxEnrollmentPermitted = 0);
bool enroll (std::string studentName);
void print (std::ostream& output) const;
};
inline
std::ostream& operator<< (std::ostream& out, const Course& c) {
c.print(out);
return out;
}
#endif
course.cpp:
#include "course.h"
#include "arrayUtils.h"
using namespace std;
Course::Course(std::string courseName, int maxEnrollmentPermitted)
: name(courseName), enrollment(0), maxEnrollment(maxEnrollmentPermitted)
{
students = new string[maxEnrollmentPermitted];
}
bool Course::enroll(std::string studentName) {
if (enrollment < maxEnrollment) {
addInOrder(students, enrollment, studentName);
return true;
}
else {
return false;
}
}
void Course::print(std::ostream& output) const {
output << name << "\n";
for (int i = 0; i < enrollment; ++i) {
output << " " << students[i] << "\n";
}
}
答案 0 :(得分:0)
课程内容:
bool operator< (const Course&);
bool operator< (const Course&) const;
bool operator== (const Course&) const;
Inside course.cpp:
bool Course::operator< (const Course& course) {
if (name < course.name)
return true;
else if (course.name < name)
return false;
if (enrollment < course.enrollment)
return true;
else if (course.enrollment < enrollment)
return false;
if (maxEnrollment < course.maxEnrollment)
return true;
else if (course.maxEnrollment < maxEnrollment)
return false;
if (students < course.students)
return true;
else if (course.students < students)
return false;
return false;
}
bool Course::operator< (const Course& course) const {
if (name < course.name)
return true;
else if (course.name < name)
return false;
if (enrollment < course.enrollment)
return true;
else if (course.enrollment < enrollment)
return false;
if (maxEnrollment < course.maxEnrollment)
return true;
else if (course.maxEnrollment < maxEnrollment)
return false;
if (students < course.students)
return true;
else if (course.students < students)
return false;
return false;
}
bool Course::operator== (const Course& course) const {
return name == course.name
&& enrollment == course.enrollment
&& maxEnrollment == course.maxEnrollment
&& students == course.students;
}
但是程序没有正确执行。仍在努力。