我试图使用我自己的析构函数而不是默认的析构函数,但无论如何我得到以下错误。有谁知道我为什么会这样做?
错误:
AssignmentRepository.o: In function `ZNSt6vectorI10AssignmentSaIS0_EE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPS0_S2_EERKS0_':
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/vector.tcc:308: undefined reference to `Assignment::~Assignment()'
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/vector.tcc:308: undefined reference to `Assignment::~Assignment()'
AssignmentRepository.o: In function `ZN9__gnu_cxx13new_allocatorI10AssignmentE7destroyEPS1_':
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/ext/new_allocator.h:118: undefined reference to `Assignment::~Assignment()'
AssignmentRepository.o: In function `ZSt8_DestroyI10AssignmentEvPT_':
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/stl_construct.h:94: undefined reference to `Assignment::~Assignment()'
collect2: ld returned 1 exit status
.h文件中的类代码:
#ifndef ASSIGNMENTREPOSITORY_H_
#define ASSIGNMENTREPOSITORY_H_
#include "Assignment.h"
#include <vector>
class AssignmentRepository{
private:
vector <Assignment> assignments;
public:
vector <Assignment> getAll();
void save(Assignment);
void editAssignment(Assignment);
int searchById(int);
void printAllAssignments();
int findByName(string name);
Assignment *getAssignment(int i);
~AssignmentRepository();
};
#endif /* ASSIGNMENTREPOSITORY_H_ */
类的.cpp文件:
int AssignmentRepository::searchById(int a){
for(unsigned i=0; i<assignments.size(); i++){
if(a == assignments[i].getID()){
return i;
}
}
return 0;
}
AssignmentRepository::~AssignmentRepository(){
}
Assignment.h类标题:
class Assignment {
private:
int id;
int grade;
int dLine;
string descrption;
public:
Assignment(int gr, int dl, string desc):grade(gr),dLine(dl),descrption(desc){};
~Assignment();
void setGrade(int value) {grade = value;}
void setDLine(int value) {dLine = value;}
void setDescription(string value) {descrption = value;}
void setID(int value) {id = value;}
int getID() const{return id;}
int getGrade() const{return grade;}
int getDLine() const{return dLine;}
string getDescription() const{return descrption;}
friend ostream& operator << (ostream& out, const Assignment& assignment)
{
out << assignment.grade << " " << assignment.dLine << " " << assignment.descrption <<endl;
return out;
}
};
#endif /* ASSIGNMENT_H_ */
答案 0 :(得分:4)
您已声明Assignment
析构函数,但尚未对其进行定义。
尝试替换此行:
~Assignment();
这一个:
~Assignment() {}