10

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?

Stefan Stojkovski
  • 479
  • 2
  • 8
  • 17

2 Answers2

45

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)

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • Oh i thought that it worked either way, with or without those brackets, and you are saying constructor but it has ~ in front so its a destructor. I know i don't have to write a constructor and if i want i would use default provided by the compiler but does it also provide a default destructor as well or i have to write ~Email() { } in order to free in order to free the the space in memory? – Stefan Stojkovski Aug 18 '13 at 19:20
  • 1
    Default constructors and destructors come for free. In C++11, this is as easy as "~Email()=default;". While your'e at it, why don't you use std::string instead of fixed-lenght char buffers for the in-class data? – Sven Aug 18 '13 at 19:51
  • Whoops, yes, typo on the destructor/constructor wording. Fixed now. – Joachim Isaksson Aug 18 '13 at 20:33
  • Can you tell what to change in order to use std:string, my program crashes if i do the change as much as i know? – Stefan Stojkovski Aug 19 '13 at 11:57
  • @StefanStojkovski Change all char* to `std::string` and use regular `=` instead of strcpy should get you started, for optimisation you'll want to pass `const std::string&` instead of plain `std::string` as parameters as much as you can get away with, but that's secondary since both work in most cases. If you have a particular other problem, maybe you could start a new question, it tends to get more attention from the debugging gurus :) – Joachim Isaksson Aug 19 '13 at 12:05
  • I thought I was allowed to have a pure virtual destructor (`= 0`) in my abstract base class, but apparently not! Changed it to `= default` and now my program runs. Thanks! – mpen Apr 10 '20 at 21:57
  • A tutorial showed me without the curly braces but landed here after the error. There are so many C++ tutorials. Which ones should noobs stick to? Ideally one that shows code examples too. – mLstudent33 May 22 '22 at 17:49
6

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.

Iowa15
  • 3,027
  • 6
  • 28
  • 35