Undefined reference to destructor error in c++?

最后都变了- 提交于 2019-12-12 09:06:42

问题


Here is the class

class Email{
private:
    char to[100];
    char from[100];
    char subject[200];
    char body[1000];

public:
    Email();
    Email(char *za,char *od,char *tema, char *telo){
    strcpy(to,za);
    strcpy(from,od);
    strcpy(subject,tema);
    strcpy(body,telo);
    }
    ~Email();
    void setTo(char *to) {strcpy(this->to,to);}
    void setFrom(char *from) {strcpy(this->from,from);}
    void setSubject(char *subject) {strcpy(this->subject,subject);}
    void setBody (char *body) {strcpy(this->body,body);}
    char* getTo () {return to;}
    char* getFrom () {return from;}
    char* getSubject () {return subject;}
    char* getBody () {return body;}
    void print () {
    cout<<"To: "<<to<<endl<<"From: "<<from<<endl<<"Subject: "<<subject<<endl<<body;
    }
};

and as you can see it includes a destructor. The rest of the program is just one function and main.

int checkEmail(char *p){
int n=0,i=0;
while(p[i]!='\0')
{if(p[i]=='@')
n++;
i++;}
if(n==1)
    return 1;
else return 0;
    }

int main()
{
    char od[100],za[100],tema[200],telo[1000];
     cout<<"Za: ";
    cin>>za;
    if(checkEmail(za)){
    cout<<"Od: ";
    cin>>od;
    cout<<"Tema: ";
    cin>>tema;
    cout<<"Poraka: ";
    cin>>telo;
    Email o(od,za,tema,telo);
    cout<<"Isprateno: ";
    o.print();
}
     else cout<<"Pogresna adresa!";
}

It gives an error

  1. obj\Debug\main.o||In function `main':|
  2. C:\Users\Stefan\Desktop\EMail\main.cpp|58|undefined reference to `Email::~Email()'|
  3. C:\Users\Stefan\Desktop\EMail\main.cpp|58|undefined reference to `Email::~Email()'|
  4. ||=== Build finished: 2 errors, 0 warnings (0 minutes, 1 seconds) ===|

in the line containing o.print(); So what is it? Also can sb. tell me how to highlight some lines in my code?


回答1:


You're declaring a destructor;

~Email();

...but not defining a body for it. Maybe you mean;

~Email() { }

...or to just leave it out if it has no functionality?

(You're also missing a body declaration for your default constructor)




回答2:


You have to define your destructor, not just declare it. There is no visible implementation. Do something like this:

~Email() {
//Whatever you want your destructor to take care of
}

If you don't want to do anything with your destructor, then just don't even declare it. Also make sure you do the same thing for your constructor. It looks like you may have the same problem with it too.



来源:https://stackoverflow.com/questions/18302617/undefined-reference-to-destructor-error-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!