0

我尝试使用我自己的析构函数而不是默认析构函数,但无论如何我都会收到以下错误。有谁知道我为什么会得到这个?

错误:

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_ */
4

1 回答 1

4

你已经声明了Assignment析构函数,但你还没有定义它。

尝试替换此行:

~Assignment();

有了这个:

~Assignment() {}
于 2012-05-15T20:59:51.723 回答