c++ 数组模板类 出现bug

nkenen 2019-03-20 01:18:40
#ifndef MyVextor_hpp__
#define MyVextor_hpp__

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>

using namespace std;


template <typename T>
class MyVector
{
friend ostream &operator<<<T>(ostream &os, MyVector &myvt);
public:
MyVector();
MyVector(T *array,int len);
virtual ~MyVector();

friend ostream &operator<<<T>(ostream &os, MyVector &myvt);
T &operator[](int index);
MyVector &operator=(MyVector &other);

int len;
T *myvp;

};

template <typename T>
MyVector<T>::MyVector(){

cout << "无参构造函数..." << endl;
}

template <typename T>
MyVector<T>::MyVector(T *array, int len){

// if (*array != nullptr)
// {
// return;
// }

this->len = len;
this->myvp = new T[len + 1];
memset(this->myvp, 0, this->len + 1);
memcpy(this->myvp,array,len*sizeof(T));

}

template <typename T>
MyVector<T>::~MyVector(){

if (this->myvp != nullptr)
{
delete[] this->myvp;
this->myvp = nullptr;
}

}

template <typename T>
ostream &operator<<(ostream &os, MyVector<T> &myvt){

for (int i = 0; i < myvt.len;i++)
{
os << myvt.myvp[i] << " ";
}
os << endl;

return os;
}

template <typename T>
T &MyVector<T>::operator[](int index){

if (index > this->len)
{
cout << "operator[] 重载error" << endl;
}
return this->myvp[index];

}

template <typename T>
MyVector<T> &MyVector<T>::operator=(MyVector<T> &other){

if (this->myvp != nullptr)
{
delete[] this->myvp;
this->myvp = nullptr;
}

this->len = other.len;
this->myvp = new T[len + 1];
memset(this->myvp, 0, this->len + 1);
memcpy(this->myvp, other.myvp, len);

return *this;
}


#endif // MyVextor_h__

上面是数组模板类


#include "MyVextor.hpp"

class Teacher
{
public:
Teacher();
Teacher(char *name,int age);
~Teacher();

friend ostream &operator<<(ostream &os,Teacher &other);

private:
char *name;
int age;

};

Teacher::Teacher()
{
}

Teacher::Teacher(char *name, int age){

this->age = age;

this->name = new char[10];
strcpy(this->name, name);

}

Teacher::~Teacher()
{
if (this->name != nullptr)
{
delete[] this->name;
this->name = nullptr;
}
}

ostream &operator<<(ostream &os, Teacher &other){

os << "Teacher " << other.name << "age " << other.age << endl;

return os;

}


int main(){

char car[] = { 'a', 'b', 'c' };
int iar[] = {1,2,3};
Teacher Tar[] = { Teacher("skskd",45),Teacher("tyty",69),Teacher("sdsdsfg",68)};


//MyVector<char> cp(car,sizeof(car)/sizeof(char));
//MyVector<int> ip(iar, sizeof(iar) / sizeof(int));
MyVector<Teacher> Tp(Tar, sizeof(Tar) / sizeof(Teacher));

//cout << cp[1] <<endl;
//cout << cp;
//cout << ip;
cout << Tp;
return 0;
}

运行是成功的,但是在析构时出现问题,直接就崩了








...全文
262 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2019-03-21
  • 打赏
  • 举报
回复
崩溃的时候在弹出的对话框按相应按钮进入调试,按Alt+7键查看Call Stack即“调用堆栈”里面从上到下列出的对应从里层到外层的函数调用历史。双击某一行可将光标定位到此次调用的源代码或汇编指令处,看不懂时双击下一行,直到能看懂为止
nkenen 2019-03-21
  • 打赏
  • 举报
回复
还是崩了 Teacher &Teacher::operator=(const Teacher &other){ // if (this->name != nullptr) // { // delete[] this->name; // this->name = nullptr; // } this->age = other.age; this->name = new char[10]; strcpy(this->name, other.name); return *this; }
nkenen 2019-03-21
  • 打赏
  • 举报
回复
this->len = len; this->myvp = new T[len + 1]; memset(this->myvp, 0, this->len + 1); for (int i = 0; i < len;i++) { this->myvp[i] = *(array + i); } Teacher &Teacher::operator=(const Teacher &other){ if (this->name != nullptr) { delete[] this->name; this->name = nullptr; } this->age = age; this->name = new char[10]; strcpy(this->name, name); return *this; } 我做了拷贝函数,可是难受的是
真相重于对错 2019-03-20
  • 打赏
  • 举报
回复
既然用c++,请不要用memcpy ,而要用他的构造函数 memset(this->myvp, 0, this->len + 1); memcpy(this->myvp, other.myvp, len); 容器里面的对像的中指针和原来数组里指针指向同一个地址。 MyVector 析构函数回delete 容器对象一次 然后 那个数组退出函数时又会delete一次, 自然出错 请百度一下 深拷贝和浅拷贝。
nkenen 2019-03-20
  • 打赏
  • 举报
回复
在Teacher 析构函数有delete name 同样在数组模板类析构函数有delete myvp,应该怎么解决呢,求大神指导

65,187

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧