麻烦理解运营商超载

问题描述:

我是不寻找任何人为我做我的编码。然而,当调试修复我的代码时,这个概念正在逃避我。麻烦理解运营商超载

我知道我期待添加一些包含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; 
} 

个第二&第三个错误:

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; 
} 

所以fo与'<' rthe错误,我知道我在寻找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

你想确定一个'Course'是否小于(或之前)另一个,并且你不想在这样做时改变'Course'对象,所以你的'operator aschepler

+0

@aschepler请参阅更新后的编辑,其中包含发生错误的'vectorUtils.h'代码块(针对每个相应的错误进行相应命名)。这会改变你的答案吗?我会想象它会。例如'addInOrder()'是'(std :: vector vectr&,T value)'。 –

+0

伙计。一次一个错误。停止移动目标帖子。投票结果太宽泛。 – user4581301

内course.h:

bool operator< (const Course&); 
bool operator< (const Course&) const; 
bool operator== (const Course&) const; 

内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; 
} 

计划但不执行正确。仍在努力。