使用析构函数后,代码显示;对书籍的引用::~Book();错误 我是C++的学习者,我进入了构造函数和析构函数的主题。我编译了下面的代码,它返回了对Book::~Book()的未定义引用错误。但是当我注释掉析构函数时,它工作得很好。我想我可以在使用析构函数后创建成员函数。我做错了什么?为了更好地理解,我在下面编写了代码 class Book { private: int *pages; int *price; public: Book() //default constructor { pages = new int; price = new int; *pages = 300; *price = 8; }; void pre_destructor() { std::cout << "The pages:" << *pages; std::cout << "The price:" << *price; } ~Book(); //destructor void post_destructor() { std::cout << "The pages:" << *pages << "\n"; std::cout << "The price:" << *price << "\n"; delete pages; delete price; } }; int main() { using namespace std; Book book1; cout << "Before using destructors" << endl; cout << "---------------------------------"<< endl; book1.pre_destructor(); cout << "After using destructors" << endl; cout << "---------------------------------"; book1.post_destructor(); return 0; } //destructor is called here 教材 { 私人: int*页; 整数*价格; 公众: Book()//默认构造函数 { pages=新int; 价格=新整数; *页数=300页; *价格=8; }; void pre_析构函数() { std::cout

使用析构函数后,代码显示;对书籍的引用::~Book();错误 我是C++的学习者,我进入了构造函数和析构函数的主题。我编译了下面的代码,它返回了对Book::~Book()的未定义引用错误。但是当我注释掉析构函数时,它工作得很好。我想我可以在使用析构函数后创建成员函数。我做错了什么?为了更好地理解,我在下面编写了代码 class Book { private: int *pages; int *price; public: Book() //default constructor { pages = new int; price = new int; *pages = 300; *price = 8; }; void pre_destructor() { std::cout << "The pages:" << *pages; std::cout << "The price:" << *price; } ~Book(); //destructor void post_destructor() { std::cout << "The pages:" << *pages << "\n"; std::cout << "The price:" << *price << "\n"; delete pages; delete price; } }; int main() { using namespace std; Book book1; cout << "Before using destructors" << endl; cout << "---------------------------------"<< endl; book1.pre_destructor(); cout << "After using destructors" << endl; cout << "---------------------------------"; book1.post_destructor(); return 0; } //destructor is called here 教材 { 私人: int*页; 整数*价格; 公众: Book()//默认构造函数 { pages=新int; 价格=新整数; *页数=300页; *价格=8; }; void pre_析构函数() { std::cout,c++,c++11,constructor,destructor,dynamic-memory-allocation,C++,C++11,Constructor,Destructor,Dynamic Memory Allocation,您的析构函数已声明,但从未定义 看起来“post_析构函数”执行实际的销毁。因此,您只需按如下方式编写析构函数: ~Book() {} // empty, nothing to do here... 前面的void pre_destructor()是没有意义的;最好放在dtor(destructor的缩写)本身, 而且post_destructor()甚至有潜在的危害 #include <iostream> class Book { private: int *page

您的析构函数已声明,但从未定义

看起来“post_析构函数”执行实际的销毁。因此,您只需按如下方式编写析构函数:

~Book() {}  // empty, nothing to do here...

前面的
void pre_destructor()
是没有意义的;最好放在dtor(destructor的缩写)本身, 而且
post_destructor()
甚至有潜在的危害

#include <iostream>

class Book
{
private:
    int *pages;
    int *price;

public:
    Book() : pages(new int(300)), price(new int(8)) {}  

    ~Book() {
        std::cout << "The pages:" << *pages << "\n";
        std::cout << "The price:" << *price << "\n";
        delete price;
        delete pages;
    } 
};

int main()
{
    {
        Book book1;
    } //destructor is called here

    return 0;
}  
#包括
课堂用书
{
私人:
int*页;
整数*价格;
公众:
Book():pages(新整数(300)),price(新整数(8)){}
~Book(){

std::不欢迎使用SO!您忘记实现析构函数;
~Book();
只是它的声明。我进行了编辑。但现在它显示“预期;在成员声明末尾”指向析构函数被称为具有特定清理功能的行通常是坏的设计,如果你必须手动调用它们,因为你可以忘记调用它们或者发生异常。考虑将它们放置在析构函数中。@ Kiran,我发现幸运的是,你想立即从C++ 11开始,因为我可以接受FR。从标记中删除。确保尽可能避免无人参与的指针,并改用。这将避免实现自定义dtor(destructor的缩写)的必要性。您似乎认为向类中添加成员的顺序在某种程度上控制了代码的运行顺序?事实并非如此。
main()中的行
调用成员函数决定了代码的运行顺序。在析构函数中肯定有一些事情要做。看看他的数据成员。他还有两个函数,分别是
pre_destructor()
post_destructor()
。他必须释放构造函数中分配的内存。是的,但他已经在“析构函数”了在post_destructor()中。我同意这里有指针,但如果他手动调用“post_destructor()”,它在调用实际的析构函数之前就已经被删除了。TBH,我不确定为什么他有前置和后置析构函数,而不仅仅是在析构函数中进行销毁…你说得对。我没有查看
main()
中的代码。