关于#c++#的问题,请各位专家解答!

人现在在实验室,这个是哪里有错了或者有bug?怎么改呀,不会不会,求你们帮忙看一下谢谢

img

仔细检查大括号的配对情况,肯定是少了一个'}'了。

  • 这个问题的回答你可以参考下: https://ask.csdn.net/questions/7612504
  • 这篇博客也不错, 你可以看下C++源代码游戏,有bug请提出
  • 除此之外, 这篇博客: 黑马程序员c++教程,利用类模板实现数组封装案例中,一种断点bug的产生原因及解决方案中的 问题描述 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 在测试代码的过程中,如果通过下标的方式访问数组元素,会出现触发断点的bug

    以下是数组类模板的实现代码:

    #pragma once
    #include <iostream>
    #include <string>
    using namespace std;
    
    template<typename T>
    class MyArray
    {
    public:
    	//构造函数(容量)
    	MyArray(int capacity);
    	//拷贝构造
    	MyArray(const MyArray& arr);
    	//operator=
    	MyArray& operator=(const MyArray& arr);
    	//尾插元素
    	void PushBack(T x);
    	//尾删元素
    	void PopBack(T x);
    	//利用[下标]访问数组元素
    	T& operator[](const int i);
    	//获取数组大小
    	int GetSize();
    	//获取数组容量
    	int GetCapacity();
    	//析构函数
    	~MyArray();
    private:
    	T* m_arr;
    	int m_size;
    	int m_capacity;
    };
    
    //构造函数(容量)
    template<typename T>
    MyArray<T>::MyArray(int capacity)
    {
    	this->m_arr = new T(capacity);
    	this->m_size = 0;
    	this->m_capacity = capacity;
    	cout << "MyArray的构造函数调用" << endl;
    }
    
    //拷贝构造函数
    //需要深拷贝
    template<typename T>
    MyArray<T>::MyArray(const MyArray<T>& arr)//
    {
    	this->m_capacity = arr.m_capacity;
    	this->m_size = arr.m_size;
    	this->m_arr = new T(m_capacity);
    	for (int i = 0; i < this->m_size; i++)
    	{
    		this->m_arr[i] = arr.m_arr[i];
    	}
    	cout << "MyArray的拷贝构造函数调用" << endl;
    }
    
    //operator=
    //同样需要深拷贝
    template<typename T>
    MyArray<T>& MyArray<T>::operator=(const MyArray<T>& arr)//
    {
    	//先检验原数组是否已经存在数据,若已存在,先删除再拷贝
    	if (this->m_arr != NULL)
    	{
    		delete[]this->m_arr;
    		this->m_size = 0;
    		this->m_capacity = 0;
    		this->m_arr = NULL;
    	}
    	this->m_capacity = arr.m_capacity;
    	this->m_size = arr.m_size;
    	this->m_arr = new T(m_capacity);
    	for (int i = 0; i < this->m_size; i++)
    	{
    		this->m_arr[i] = arr.m_arr[i];
    	}
    	cout << "MyArray的operator=函数调用" << endl;
    	return *this;
    }
    
    //尾插元素
    template<typename T>
    void MyArray<T>::PushBack(T x)
    {
    	if (this->m_size == this->m_capacity)
    	{
    		cout << "当前数组已满,无法插入!" << endl;
    	}
    	else
    	{
    		this->m_arr[this->m_size] = x;
    		(this->m_size)++;
    	}
    }
    
    //尾删元素
    template<typename T>
    void MyArray<T>::PopBack(T x)
    {
    	if (this->m_size == 0)
    	{
    		cout << "当前数组为空,无法删除!" << endl;
    	}
    	else
    	{
    		(this->m_size)--;
    	}
    }
    
    //利用[下标]访问数组元素
    template<typename T>
    T& MyArray<T>::operator[](const int i)
    {
    	return this->m_arr[i];
    }
    
    //获取数组大小
    template<typename T>
    int MyArray<T>::GetSize()
    {
    	return this->m_size;
    }
    //获取数组容量
    template<typename T>
    int MyArray<T>::GetCapacity()
    {
    	return this->m_capacity;
    }
    
    //析构函数
    template<typename T>
    MyArray<T>::~MyArray()
    {
    	delete[]this->m_arr;
    	this->m_size = 0;
    	this->m_capacity = 0;
    	this->m_arr = NULL;
    	cout << "MyArray的析构函数调用" << endl;
    }
    

    以下是测试代码:

    int main()
    {
    	MyArray<int> arr(5);
    	int capacity = arr.GetCapacity();
    	for (int i = 0; i < capacity; i++)
    	{
    		arr.PushBack(i);
    	}
    
    	int size = arr.GetSize();
    	for (int i = 0; i < size; i++)
    	{
    		cout << arr[i]<<endl;
    	}
    }
    

    以下是出现的断点bug
    在通过“[下标]”的方式访问利用类模板创建的数组时,触发断点在通过“[下标]”的方式访问利用类模板创建的数组时,触发断点
    最初笔者以为,是因为重载[]运算符的代码有误,导致无法通过方括号内下标的方式访问数组,但将这段代码注释掉,即不使用重载的运算符后,出现了更严重的bug:
    在这里插入图片描述