帮我检查下这段代码哪里有问题:// #pragma once #include<iostream> using namespace std; template<class T> class Myarray { public: Myarray(int Capa) { cout << "M的有残调用" << endl; this->m_Capay = Capa; this->m_size = 0; this->pAdress = new T[this->m_Capay] } Myarray(const Myarray& arr) { cout << "M的拷贝调用" << endl; this->m_Capay = arr.m_Capay; this->m_size = arr.m_Capay; //this->pAdress = arr.pAdress; this->pAdress = new T[arr.m_Capay]; for (int i = 0; i < this->m_size; i++) { this->pAdress[i] = arr.pAdress[i]; } } Myarray& operator=(const Myarray& arr) { cout << "M的有operat用" << endl; if (this->pAdress != NULL) { delete[] this->pAdress; this->pAdress = NULL; this->m_Capay = 0; this->m_size = 0 } this->m_Capay = arr.m_Capay; this->m_size = arr.m_size; this->pAdress = new T[arr.m_Capay]; for (int i = 0; i < m_size; i++) { this->pAdress[i] = arr.pAdress[i]; } return *this; } ~Myarray() { if (this->pAdress != NULL) { cout << "M的西沟调用" << endl; delete[] this->pAdress; this->pAdress = NULL; } } private: T* pAdress; int m_Capay; int m_size; };
时间: 2024-01-31 09:04:17 浏览: 175
#pragma用法讲解.pdf
在第13行,构造函数的语句 `this->pAdress = new T[this->m_Capay]` 末尾缺少分号`;`,应该修改为:
```
this->pAdress = new T[this->m_Capay];
```
在第27行,赋值运算符的语句 `this->m_size = arr.m_Capay;` 应该修改为:
```
this->m_size = arr.m_size;
```
在第34行,赋值运算符的语句 `this->m_size = arr.m_size;` 应该修改为:
```
this->m_size = arr.m_size;
```
在第38行,析构函数的语句 `this->m_size = 0` 末尾缺少分号`;`,应该修改为:
```
this->m_size = 0;
```
修改后的代码如下:
```
#pragma once
#include<iostream>
using namespace std;
template<class T>
class Myarray {
public:
Myarray(int Capa) {
cout << "M的有残调用" << endl;
this->m_Capay = Capa;
this->m_size = 0;
this->pAdress = new T[this->m_Capay];
}
Myarray(const Myarray& arr) {
cout << "M的拷贝调用" << endl;
this->m_Capay = arr.m_Capay;
this->m_size = arr.m_size;
//this->pAdress = arr.pAdress;
this->pAdress = new T[arr.m_Capay];
for (int i = 0; i < this->m_size; i++)
{
this->pAdress[i] = arr.pAdress[i];
}
}
Myarray& operator=(const Myarray& arr) {
cout << "M的有operat用" << endl;
if (this->pAdress != NULL)
{
delete[] this->pAdress;
this->pAdress = NULL;
this->m_Capay = 0;
this->m_size = 0;
}
this->m_Capay = arr.m_Capay;
this->m_size = arr.m_size;
this->pAdress = new T[arr.m_Capay];
for (int i = 0; i < m_size; i++)
{
this->pAdress[i] = arr.pAdress[i];
}
return *this;
}
~Myarray() {
if (this->pAdress != NULL)
{
cout << "M的西沟调用" << endl;
delete[] this->pAdress;
this->pAdress = NULL;
}
}
private:
T* pAdress;
int m_Capay;
int m_size;
};
```
阅读全文