[C++]缺少析构函数

[c++]Missing of destructor

本文关键字:析构函数 C++      更新时间:2023-10-16

当我像这样声明一个新的队列对象时,exe停止工作:

arrayQueue myQueue;  
myQueue.enqueue("111");  
char* x=myQueue.dequeue();  
cout<<x<<endl;

当我使用 new 创建对象时,它可以工作:

arrayQueue* myQueue=new arrayQueue();  
myQueue->enqueue("111");  
char* x=myQueue->dequeue();  
cout<<x<<endl;

那么问题出在哪里呢? 以下代码是我编写的"队列":

在 .h 头文件中:

class arrayQueue{  
private:  
    array<char*,100> queueContrainer;  
    int maxSize;  
    int head;  
    int tail;  
public:  
    arrayQueue();  
    ~arrayQueue();  
    bool isEmpty();  
    bool isFull();  
    int getSize();  
    void enqueue(char*);  
    char* dequeue();  
};

.cpp中的实现(仅在此处上传构造函数:

arrayQueue::arrayQueue(){  
    head=0;  
    tail=0;  
    maxSize=100;  
    for(array<char*,100>::iterator it1=queueContrainer.begin();it1!=queueContrainer.end();++it1){  
        *it1="Empty";  
    }  
}
main.cpp; arrayQueue

.cpp; arrayQueue.h. 三个要编译的文件:


https://drive.google.com/file/d/0B5FCKG1I8ce0R1RORUFYWFhUN0E/edit?usp=sharing
https://drive.google.com/file/d/0B5FCKG1I8ce0QlBCTzdBUlJfZG8/edit?usp=sharing https://drive.google.com/file/d/0B5FCKG1I8ce0SGdWOVg1RzNTNW8/edit?usp=sharing

你有有效的析构函数吗?在第二个示例中,myQueue指向的对象不会按预期删除,并且永远不会调用析构函数。

在第一个示例中,当myQueue超出范围时将被删除,并且将自动调用析构函数。如果您的析构函数丢失或有错误,程序将无法编译或运行。

在第一行

queue myQueue;

您正在创建一个类型为"queue"的对象,可能是 std::queue,它没有"enqueue"或"dequeue"方法。