Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined reference to destructor error in c++?

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?

like image 300
Stefan Stojkovski Avatar asked Aug 18 '13 18:08

Stefan Stojkovski


2 Answers

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)

like image 96
Joachim Isaksson Avatar answered Nov 13 '22 19:11

Joachim Isaksson


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.

like image 25
Iowa15 Avatar answered Nov 13 '22 19:11

Iowa15