类模板实现自定义数据类型的存储

今天在练习C++类模板数组封装案例时,遇到了一个不解的问题
首先,我构造了一个类模板,想要实现内置数据类型和自定义数据类型数据的存储,但是在写完代码之后,发现无法实现关于自定义数据类型的存储,系统一直报错,
test01是关于类内置数据类型的存储,其可以成功运行,如下所示

img

test02是关于自定义数据类型的存储,经过多次实验,我发现错误出在这一句上,即myArrayarr(10);
但具体原因由于自身能力有限,还在探究之中,希望看到的友友指点一二

img

以下是关于类模板的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指针指向的内存空间中。

以下是修复问题的具体步骤和代码示例:

  1. 在类模板的私有成员变量中,增加一个T*类型的指针变量pAddress,用于保存动态分配的数组内存地址。
  2. 修改构造函数,为pAddress动态分配内存,并将m_Size和m_Capacity初始化为0。代码如下:
myArray(int capacity)
{
    cout << "有参构造调用" << endl;
    this->m_Capacity = capacity;
    this->m_Size = 0;
    this->pAddress = new T[this->m_Capacity];
}
  1. 修改拷贝构造函数,进行深拷贝。代码如下:
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];
    }
}

完成上述步骤后,你的类模板应该能够正确地存储自定义数据类型了。



【相关推荐】


  • 你可以看下这个问题的回答https://ask.csdn.net/questions/7711812
  • 除此之外, 这篇博客: 指针和数组试题解析(重置版)中的 小编,在这里想说一下,c语言的最后一节 C预处理,可能还需要一些时间,因为小编,昨天才下载了虚拟机 和 linux 系统,还没开始安装。所以无法着手写,因为 C预处理,vs2013很难表达,也就意味可能会讲不清楚。所以这篇文章可能需要点时间,再加上小编初期的文章,是没有排版的(而且可能有些错误,请大家以重置版为准),所以这几天我就把这些重新写。有兴趣的朋友可以看看。(ps:如果哪一天没有更新,意味着小编正在努力学习,为了能给大家呈现一片详细好懂的文章。) 部分也许能够解决你的问题。

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^