重载运算符+编译错误''没有匹配函数来调用...''

时间:2012-12-28 17:38:54

标签: c++ compilation compiler-errors operator-overloading codeblocks

我正在尝试为重载operator +创建一个函数,所以我试试这个:

 // Joins eClasses.
        eClass operator+(const eClass& cls)const{
            eClass temp;
            temp.teacher=teacher;
            for(int i=0; i<=student_count;++i){
            temp.students[i]=students[i];
            }
            for(int i=0; i<=student_count;++i){
                if(temp.students[i]!=cls.students[i]){
                    temp.students[i]=cls.students[i];
                }
            }
            return temp;

这段代码在我的名为eClass的类中 这是我的eClass的原型版本,没有功能的实现

class eClass {
    private:
    Teacher* teacher;
    string eclass_name;
    Student* students[MAX_CLASS_SIZE];
    unsigned int student_count;
    public:
    eClass(Teacher&, string);
    ~eClass(); // Should free all dynamically-allocated
    // memory.
    bool exists(Student); // Returns TRUE if a student exists
    // in students[];
    // otherwise returns FALSE.
    void add(Student&); // Add a student in students[].

eClass operator+(eClass&); // Joins eClasses.

    Teacher getTeacher(void) { return *teacher; }
    string getEclassName(void) { return eclass_name; }
    unsigned int getStudentCount(void) { return student_count; }
    Student getStudent(int i) { return *students[i-1]; }
    };

所以编译器给了我那个错误:

In member function 'eClass eClass::operator+(eClass)
line 127 error: no matching function for call to 'eClass::eClass()
line 95  note:candidates are: eClass::eClass(Teacher*, std::string)
line 87  note:eClass::eClass(const eClass&)

1 个答案:

答案 0 :(得分:2)

您没有默认的ctor。定义它或构造正确:

eClass temp(cls); // or eClass temp(*this);