简体   繁体   English

带有复制构造函数的析构函数

[英]Destructor with copy constructor

I want to use destructor in CPP, but, compiler is giving warning "undefined reference".我想在 CPP 中使用析构函数,但是编译器给出警告“未定义的引用”。

class trial{
private:
    int number;
public:
    trial(){};
    trial(int num) {
        number=num;
    };
    ~trial();
};

Trial class constructor gets value from main function.试用 class 构造函数从主 function 获取值。 Help me by telling me whats incorrect in constructor.通过告诉我构造函数中有什么不正确来帮助我。 Thanks in advance:)提前致谢:)

It seems you declared the destuctor ~trial();看来您声明了析构函数~trial(); , but you did not define that. ,但你没有定义

To define the destructor inside the class declaration, use {} instead of ;要在 class 声明中定义析构函数,请使用{}而不是; like ~trial(){} .~trial(){}

To define the destructor outside the class declaration (inside a source file), write like this:要在 class 声明之外(在源文件中)定义析构函数,可以这样写:

trial::~trial() {
    // do what you want
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM