项目触发了一个断点,后跟_BLOCK_TYPE_IS_VALID(pHeap->nBlockUse)

Project has triggered a breakpoint followed by _BLOCK_TYPE_IS_VALID(pHeap->nBlockUse)

本文关键字:VALID IS BLOCK TYPE nBlockUse gt pHeap- 项目 断点 一个 后跟      更新时间:2023-10-16

我在一个自实现的动态向量上定义了一个ADT集合。当我试图在ADT中放入元素时,"Project已触发断点。"异常发生在visual studio中,或者代码块中出现"应用程序停止工作…"。我认为方法很简单,有人能指出我做错了什么吗?谢谢

动态矢量编码:

#ifndef DYNAMICVECTOR_H
#define DYNAMICVECTOR_H
template<class T>class DynamicVector{
public:
DynamicVector(int dim){
    this->vect = new T[dim];
    this->dim = dim;
    this->index = -1;
}
~DynamicVector(){
    delete [] vect;
}
void addElement(T elem){
    index++;
    this->vect[index] = elem;
}
private:
    T *vect;
    int index, dim;
};
#endif

ADT集合代码:

#ifndef COLLECTION_H
#define COLLECTION_H
#include "DynamicVector.h"
#include "IteratorD.h"
template<class T>class Collection{
private:
DynamicVector<T> vect;
public:
Collection(){
    this->vect = DynamicVector<T>(10);
}
void add(T elem){
    this->vect.addElement(elem);
}
};
#endif

和main.cpp:

#include "DynamicVector.h"
#include "Collection.h"
#include <iostream>
using namespace std;
void main()
{
Collection<int> col = Collection<int>();
    col.add(1);
    col.add(1);
    col.add(1);
    system("pause");
}

您的代码没有使用gcc 4.8.1编译(没有任何标志(,原因有二:

  1. CCD_ 1生成错误。它应该是int main()

  2. 由于您没有在Collection构造函数中使用初始化列表,因此DynamicVector中需要一个默认构造函数,该构造函数无法自动生成,因为您提供了一个带int的重载构造函数。
    更改为Collection() : vect(10) {}

现在,您的代码编译并运行良好。如果Visual Studio按照您的问题中提供的那样编译得很好,那么请迅速逃离它。

我认为这里的问题归结为如何初始化成员。您需要做的是使用初始化列表语法来初始化CollectionDynamicVector的成员。有关更多信息,我将引导您访问此c++常见问题解答项目:http://www.parashift.com/c++-faq/init-lists.html

template<class T>
class DynamicVector{
    public:
        DynamicVector(int dim):
            dim(dim),
            index(-1)        
        {
            this->vect = new T[dim];
        }
        ~DynamicVector(){
            delete [] vect;
        }
        void addElement(T elem){
            index++;
            this->vect[index] = elem;
        }
    private:
        T *vect;
        int dim;
        int index;
};
template<class T>
class Collection{
    private:
        DynamicVector<T> vect;
    public:
        Collection():
            vect(10) //use this way to initialize vect
        {
        }
        void add(T elem){
            this->vect.addElement(elem);
        }
};
int main(){
Collection<int> col = Collection<int>();
    col.add(1);
    col.add(1);
    col.add(1);
    return 0;
}

此外,请使用int main()而不是void main(),请参阅http://www.parashift.com/c++-faq lite/main-returns-int.html

Collection(){
    this->vect = DynamicVector<T>(10);
}

这是故障代码。因为您正在创建DynamicVector(10(的本地对象,然后使用编译器生成的默认复制构造函数将其分配给变量this->vect。此默认的复制构造函数将执行成员式赋值。然后本地对象实例被销毁,它将调用DynamicVector的析构函数。这将通过调用delete[]来释放内存。

因此,现在DynamicVector中由该->vect持有地址的内存是无效的。稍后,当您试图通过取消引用指针来访问它时,就会出现错误。

解决方案:

template<class T>class Collection{
private:
DynamicVector<T> * vect;
public:
Collection(){
    this->vect = new DynamicVector<T>(10);
}
~Collection() {
    delete this->vect;
}
void add(T elem){
    this->vect->addElement(elem);
}
};