c++ 中对析构函数错误的未定义引用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18302617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:52:50  来源:igfitidea点击:

Undefined reference to destructor error in c++?

c++classdestructor

提问by Stefan Stojkovski

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]!='
~Email();
') {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) ===|
  1. obj\Debug\main.o||在函数`main'中:|
  2. C:\Users\Stefan\Desktop\EMail\main.cpp|58|未定义对`Email::~Email()'的引用|
  3. C:\Users\Stefan\Desktop\EMail\main.cpp|58|未定义对`Email::~Email()'的引用|
  4. ||=== 构建完成:2 个错误,0 个警告(0 分 1 秒)===|

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

在包含 o.print() 的行中;那是什么?也可以sb。告诉我如何突出显示代码中的某些行?

回答by Joachim Isaksson

You're declaring a destructor;

你在声明一个析构函数;

~Email() { }

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

...但没有为它定义一个主体。也许你的意思是;

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

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

...或者如果它没有功能就将其排除在外?

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

(您还缺少默认构造函数的主体声明)

回答by Iowa15

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

你必须定义你的析构函数,而不仅仅是声明它。没有可见的实现。做这样的事情:

##代码##

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.

如果您不想对析构函数做任何事情,那么甚至不要声明它。还要确保对构造函数执行相同的操作。看起来你也可能有同样的问题。