今天在练习C++类模板数组封装案例时,遇到了一个不解的问题
首先,我构造了一个类模板,想要实现内置数据类型和自定义数据类型数据的存储,但是在写完代码之后,发现无法实现关于自定义数据类型的存储,系统一直报错,
test01是关于类内置数据类型的存储,其可以成功运行,如下所示
test02是关于自定义数据类型的存储,经过多次实验,我发现错误出在这一句上,即myArrayarr(10);
但具体原因由于自身能力有限,还在探究之中,希望看到的友友指点一二
以下是关于类模板的hpp文件
#pragma once
#include<iostream>
using namespace std;
template<class T>
class myArray {
public:
//myArray(const T& ar);
//有参构造 参数 容量
myArray(int capacity)
{
cout << "有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
//拷贝构造
myArray(const myArray& arr)
{
cout << "Myarry有参拷贝构造调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
//this->pAddress = arr.pAddress;
//深拷贝
this->pAddress = new T[arr.m_Capacity];
//将arr中的数据都拷贝过来
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
}
//opreate =防止浅拷贝问题
myArray& operator=(const myArray& arr)
{
cout << "Myarray的operate=构造调用" << endl;
//先判断原来堆区是否有数据 如果有先释放
if (this->pAddress != NULL)
{
delete[]this->pAddress;
this->m_Capacity =0;
this->m_Size = 0;
}
//深拷贝
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
return *this;
}
//通过下标方式访问数据
T& operator[](int index)
{
return this->pAddress[index];
}
//尾插法
void Push_back(const T & val) {
//判断容量是否等于大小
if (this->m_Capacity == this->m_Size){
return;
}
this->pAddress[this->m_Size] = val;
this->m_Size++;//更新数组大小
}
//尾删法
void pop_back()
{
//让用户访问不到最后一个元素 即为尾删,逻辑删除
if (this->m_Size == 0) {
return;
}
this->m_Size--;
}
//返回数组的容量
int getCapacity() {
return this->m_Capacity;
}
//返回数组的大小
int getSize() {
return this->m_Size;
}
//析构函数
~myArray() {
cout << "Myarray析构函数调用" << endl;
if (this->pAddress != NULL) {
delete[]this->pAddress;
this->pAddress = NULL;
}
}
private:
T* pAddress;//指针指向堆区开辟的真实数组
int m_Size;
int m_Capacity;
};
以下是关于类模板的cpp文件
#include<iostream>
using namespace std;
#include"myArray.hpp"
#include<string>
void showarr(myArray<int>&arr) {
for (int i = 0; i < arr.getSize(); i++) {
cout << arr[i] << endl;
}
}
void test01() {
myArray<int> arr1(5);
for (int i = 1; i < 5; i++) {
arr1.Push_back(i);
}
cout << "arr1的打印输出为" << endl;
showarr(arr1);
/*myArray<int>arr2(arr1);
myArray<int>arr3(100);
arr3 = arr1;*/
cout << "arr1的容量为" <<arr1.getCapacity()<< endl;
cout << "arr1的大小为" << arr1.getSize()<<endl;
myArray<int>arr2(arr1);
cout << "arr2的打印输出为" << endl;
showarr(arr2);
//尾删法
arr2.pop_back();
cout << "arr2的容量为" << arr2.getCapacity() << endl;
cout << "arr2的大小为" << arr2.getSize() << endl;
}
//测试自定义数据类型
class person {
public:
person();
person(string name, int age) {
this->m_age = age;
this->m_name = name;
}
string m_name;
int m_age;
};
void showperson(myArray<person>& arr3) {
for (int i = 0; i < arr3.getSize(); i++) {
cout << "姓名:" << arr3[i].m_name << endl;
cout << "年龄:" << arr3[i].m_age << endl;
}
}
void test02() {
myArray<person>arr(10);
//person p1("孙悟空", 999);
//person p2("孙悟空", 999);
//person p3("孙悟空", 999);
//person p4("孙悟空", 999);
////将数据插入到数组中
//arr.Push_back(p1);
//arr.Push_back(p2);
//arr.Push_back(p3);
//arr.Push_back(p4);
////打印数组
//showperson(arr);
////输出容量
//cout<<arr.getCapacity() << endl;
////输出大小
//cout << arr.getSize()<< endl;
//
}
int main(){
test01();
test02();
system("pause");
return 0;
}
【以下回答由 GPT 生成】
这个问题可能是由于类模板的构造函数和拷贝构造函数没有正确实现导致的。你的构造函数和拷贝构造函数应该正确地管理内存,并将自定义数据类型的数据正确地复制到pAddress指针指向的内存空间中。
以下是修复问题的具体步骤和代码示例:
myArray(int capacity)
{
cout << "有参构造调用" << endl;
this->m_Capacity = capacity;
this->m_Size = 0;
this->pAddress = new T[this->m_Capacity];
}
myArray(const myArray& arr)
{
cout << "MyArray有参拷贝构造调用" << endl;
this->m_Capacity = arr.m_Capacity;
this->m_Size = arr.m_Size;
this->pAddress = new T[arr.m_Capacity];
for (int i = 0; i < this->m_Size; i++)
{
this->pAddress[i] = arr.pAddress[i];
}
}
完成上述步骤后,你的类模板应该能够正确地存储自定义数据类型了。
【相关推荐】