在我的主类中使用动态数组时出错 我是C++编程新手,尝试学习动态数组。 我的书中有一个列表调用代码,我正在尝试使用主类中的函数。 执行此操作时出现以下错误
调试错误:“CRT检测到应用程序在堆缓冲区结束后写入内存 测试列表.h在我的主类中使用动态数组时出错 我是C++编程新手,尝试学习动态数组。 我的书中有一个列表调用代码,我正在尝试使用主类中的函数。 执行此操作时出现以下错误,c++,dynamic-arrays,C++,Dynamic Arrays,调试错误:“CRT检测到应用程序在堆缓冲区结束后写入内存 测试列表.h #include "stdafx.h" #include "List.h" #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { List d; List *arr = &d; size_t user_input; //arr1 = new List(5); cout << "Enter
#include "stdafx.h"
#include "List.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
List d;
List *arr = &d;
size_t user_input;
//arr1 = new List(5);
cout << "Enter the value in to your array: ";
cin >> user_input;
arr->append(user_input);
return 0;
}
// List.cpp : Defines the entry point for the console application.
// List.cpp : Defines the entry point for the console application.
#包括“stdafx.h”
#包括“List.h”
#包括
使用名称空间std;
int _tmain(int argc,_TCHAR*argv[]
{
名单d;
列表*arr=&d;
用户输入的大小;
//arr1=新列表(5);
cout>用户输入;
arr->append(用户输入);
返回0;
}
//List.cpp:定义控制台应用程序的入口点。
//List.cpp:定义控制台应用程序的入口点。
//
#包括“stdafx.h”
#包括“List.h”
#包括
使用名称空间std;
列表::列表(大小和容量)
{
数据=新的整数[容量];
容量=容量;
尺寸=0;
}
列表::列表(常量列表和列表)
{
副本(清单);
}
列表::~List()
{
库特容量()
{
调整大小(大小+列表大小);
}
对于(i=0;i容量。
变成
data_ = new int[capacity];
或者在分配数据之前分配容量
您错过了容量的初始化:
List::List(size_t capacity) :
size_(0), data_(new int[capacity]), capacity_(capacity)
{
}
注意:您应该使用初始化列表来初始化构造函数中的成员
data_ = new int[capacity];
List::List(size_t capacity) :
size_(0), data_(new int[capacity]), capacity_(capacity)
{
}