演算子 + をオーバーロードする関数を作成しようとしているので、これを試します。
// 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&)