gpt4 book ai didi

c++ - 由于未分配正在释放的指针错误,对象的初始化 vector 失败

转载 作者:行者123 更新时间:2023-11-28 04:30:59 37 4
gpt4 key购买 nike

<分区>

下面是我创建的示例类,都需要创建下面类的对象 vector

#include <stdio.h>
#include "myvector.h"
#include <iostream>

class myvector
{
private:
double *elem;
int sz;

public:
myvector()
{
std::cout << " In Constructor \n";
}

~myvector()
{
std::cout << " In Destructor \n";
delete[] elem;
}

myvector(int s)
{
std::cout << " In Param Cons \n";
elem= new double[s];
}
myvector::myvector(const myvector& that)
{
this->sz=that.sz;
this->elem=that.elem;
}

myvector& operator=(const myvector& that)
{
return *this;
}
};

下面是主要功能

#include <iostream>
#include "myvector.h"
#include <vector>

int main(int argc, const char * argv[]) {
// insert code here...
myvector abc(10);
std::vector<myvector> abc(10,10);
getchar();
return 0;
}
myvector abc[10];  works perfectly and creates an array of objects

但是因为我需要为下面使用的所有这些对象调用参数构造函数

std::vector abc(10,10);

这实际上不是在创建一个数组,而是因为错误而失败

vector(16828,0x10013e380) malloc: * error for object 0x100400050: pointer being freed was not allocated * set a breakpoint in malloc_error_break to debug

参数函数连调用10次都没有,只调用了一次

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com