当我这样声明一个新的队列对象时,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
您有有效的析构函数吗?在第二个示例中,by指向的对象myQueue
没有按应有的方式被删除,并且析构函数从不被调用。
在第一个示例myQueue
中,当它超出范围时将被删除,并且析构函数将被自动调用。如果您的销毁丢失或有故障,该程序将无法编译或运行。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句