C++ 模板类析构函数崩溃程序中的动态数组
我有一个模板类,它包含模板类型的私有动态数组。大体上,我有一个while循环,它根据用户输入的初始条件继续运行,这一切都很好(一直在读取/操作数组) 然后在while循环结束后,数组就不再使用了。但我的程序仍在写最后2个couts和文本文件,然后运行一个简单的lambda函数(不使用数组或其类),但在完成之前崩溃:C++ 模板类析构函数崩溃程序中的动态数组,c++,templates,destructor,dynamic-arrays,C++,Templates,Destructor,Dynamic Arrays,我有一个模板类,它包含模板类型的私有动态数组。大体上,我有一个while循环,它根据用户输入的初始条件继续运行,这一切都很好(一直在读取/操作数组) 然后在while循环结束后,数组就不再使用了。但我的程序仍在写最后2个couts和文本文件,然后运行一个简单的lambda函数(不使用数组或其类),但在完成之前崩溃: newFile.close(); return 0; 根据调试器,程序计数器停止在数组的析构函数的末尾,这实际上并没有带走任何功能,但我不明白为什么它会崩溃,而不是仅仅结束 我的队
newFile.close();
return 0;
根据调试器,程序计数器停止在数组的析构函数的末尾,这实际上并没有带走任何功能,但我不明白为什么它会崩溃,而不是仅仅结束
我的队列类型。h:
template<class Type>
class QueueType {
public:
QueueType();
~QueueType();
QueueType(const QueueType& other);
Type& getFront() {return queueArray[front];}
void reposition();
void addElement(Type);
bool isEmpty() const {return numElements == 0;}
bool isFull() const {return SIZE == numElements;}
void updateWaitTimes(Type*&, int&, int&);
QueueType<Type>& operator=(const QueueType other);
friend void swap(QueueType& first, QueueType& second) {
using std::swap;
swap(first.front, second.front);
swap(first.back, second.back);
swap(first.numElements, second.numElements);
swap(first.queueArray, second.queueArray);
}
private:
static const int SIZE = 25;
int front, back, numElements;
Type *queueArray;
};
模板
类队列类型{
公众:
QueueType();
~QueueType();
队列类型(常量队列类型和其他);
键入&getFront(){return queueArray[front];}
无效重新定位();
无效元素(类型);
bool isEmpty()常量{return numElements==0;}
bool isFull()常量{return SIZE==numElements;}
void updateWaitTimes(类型*&,int&,int&);
QueueType&运算符=(const QueueType other);
好友无效交换(队列类型&第一,队列类型&第二){
使用std::swap;
交换(第一。前,第二。前);
交换(第一个。返回,第二个。返回);
互换(第一种。货币,第二种。货币);
交换(第一个.queueArray,第二个.queueArray);
}
私人:
静态常数int SIZE=25;
内前,后,Numements;
类型*队列数组;
};
我还在这个类中实现规则3(加上所示的构造函数),如下所示:
template<class Type>
QueueType<Type>::QueueType() {
queueArray = new Type[SIZE];
front = back = numElements = 0;
}
template<class Type>
QueueType<Type>::~QueueType() {
delete [] queueArray;
}
template<class Type>
QueueType<Type>::QueueType(const QueueType& other) {
front = other.front;
back = other.back;
numElements = other.numElements;
std::copy(other.queueArray, other.queueArray + SIZE, queueArray);
}
template<class Type>
QueueType<Type>& QueueType<Type>::operator=(const QueueType other) {
swap(*this, other);
return *this;
}
模板
QueueType::QueueType(){
queueArray=新类型[大小];
前=后=数值=0;
}
模板
QueueType::~QueueType(){
删除[]队列数组;
}
模板
QueueType::QueueType(常量QueueType和其他){
前=其他。前;
back=other.back;
numElements=其他。numElements;
std::copy(other.queueArray,other.queueArray+SIZE,queueArray);
}
模板
QueueType&QueueType::operator=(常量QueueType其他){
掉期(*本,其他);
归还*这个;
}
使用此主菜单:
typedef void(*Action)();
void iterateQueue(Action action) {
action();
}
int main(int argc, char** argv) {
QueueType<CustomerType> Line;
while (... < ...) {
//various functions operate on Line, which produce no problems
}
cout << ...
newFile << ...
iterateQueue([] () {cout << endl << "I'm a lambda function!" << endl;});
cout << "this never prints";
return 0;
}
typedef void(*Action)();
void iterateQueue(操作){
动作();
}
int main(int argc,字符**argv){
排队型线路;
而(…<…){
//各种功能在线运行,不会产生任何问题
}
cout您的复制构造函数错误:
template<class Type>
QueueType<Type>::QueueType(const QueueType& other) {
front = other.front;
back = other.back;
numElements = other.numElements;
std::copy(other.queueArray, other.queueArray + SIZE, queueArray);
}
模板
QueueType::QueueType(常量QueueType和其他){
前=其他。前;
back=other.back;
numElements=其他。numElements;
std::copy(other.queueArray,other.queueArray+SIZE,queueArray);
}
您没有为当前对象分配queueArray。请尝试以下操作:
template<class Type>
QueueType<Type>::QueueType(const QueueType& other) :
queueArray(new Type[SIZE]),
front(other.front),
back(other.back),
numElements(other.numElements)
{
std::copy(other.queueArray, other.queueArray + SIZE, queueArray);
}
模板
QueueType::QueueType(常量QueueType和其他):
queueArray(新类型[大小]),
前(其他前),
背面(其他背面),
货币单位(其他货币单位)
{
std::copy(other.queueArray,other.queueArray+SIZE,queueArray);
}
请提供代码,而不是用扩展的英语描述您的代码。我显示了我的析构函数代码和main中剩余的未运行的代码,不确定这个问题还需要显示什么=/您应该发布一个复制问题的最小示例。否则,唯一的答案是“您的代码中有一个bug”。你的复制构造函数完全崩溃了。除非你是专家,否则请不要使用new
和delete
。请使用合适的动态容器。Hhmm我从一篇关于3 o o o规则的文章中获得了复制/分配设置修复技巧?不知何故,它在NetBeans上编译得很好,只是在运行后崩溃这就是问题所在…更好的修复方法是使用std::vector
。当前,如果类型
复制构造函数抛出,则可能会泄漏queueArray
。感谢,这对我来说确实有意义,因为我最初没有为新数组分配堆空间,但我更改了它,并且在它运行后仍然崩溃?使用cout的我看到了该复制构造or和重载=从未调用过,析构函数最后只调用一次