项目触发了一个断点,后跟_BLOCK_TYPE_IS_VALID(pHeap->nBlockUse)
Project has triggered a breakpoint followed by _BLOCK_TYPE_IS_VALID(pHeap->nBlockUse)
我在一个自实现的动态向量上定义了一个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编译(没有任何标志(,原因有二:
-
CCD_ 1生成错误。它应该是
int main()
。 -
由于您没有在
Collection
构造函数中使用初始化列表,因此DynamicVector
中需要一个默认构造函数,该构造函数无法自动生成,因为您提供了一个带int的重载构造函数。
更改为Collection() : vect(10) {}
现在,您的代码编译并运行良好。如果Visual Studio按照您的问题中提供的那样编译得很好,那么请迅速逃离它。
我认为这里的问题归结为如何初始化成员。您需要做的是使用初始化列表语法来初始化Collection
和DynamicVector
的成员。有关更多信息,我将引导您访问此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);
}
};
- Why is UINT32_MAX + 1 = 0?
- C++错误:"error: int aaa::bbb is protected within this context"
- 创建具有 new in 函数和"this is nullptr"异常的对象
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- Is !NaN not a NaN?
- Directx 11 - CompileFromFile() is not compiling
- Centos7 g++ "to_string is not in a member of std"
- 无法创建 DLL:获取 DLL "is not a valid Win32 application"
- C++ DLL 运行时错误"abc.dll is not a valid WIN32 application" 。请帮助解决这个问题
- 我的代码应该接受一个数字,并返回字母等级或"Grade is not valid"但 else 语句不起作用
- Is std::initializer_list{x, y, z} (CTAD) valid?
- 在 Unity 中使用 x64 c++ dll 可以"Is not a valid Win32 application error"
- JNI C++ DLL - 'UnsatisfiedLinkError: %1 is not a valid Win32 application'
- getaddrinfo() 返回错误"The requested name is valid, but no data of the requested type was found."
- 在 C++ 中从 WMI(Win32_LogicalDisk 类)调用 Chkdsk -> 'Parameter is not valid'
- 无法在 NetBeans IDE 中编译C++ - 出现错误"CreateProcess error=193, %1 is not a valid Win32 application"
- 具有已定义宏的 iOS 预处理条件会导致"token is not valid"
- 错误:"CreateProcess error=193, %1 is not a valid win 32 application" 中
- 错误"token is not a valid binary operator in a preprocessor subexpression"
- 验证函数中的文件并在主文件中调用它,输出"file is valid"它是否有效