- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在这段代码中遇到了类的析构函数问题。它说从未分配过,但应该分配过,而且我自己也从不删除它。以下是代码片段:
#ifdef UNIT_TESTING_CONSTRUCTORS
//Test Constructors
cout << "Test constructors \nConctructor 1:\n";
Doctor testDoc1;
testDoc1.displayPatientArray();
cout << "\nConstructor 2:\n";
Doctor testDoc2(2);
testDoc2.displayPatientArray();
cout << "\nConstructor 3:\n";
//Implement more test cases below:
Doctor testDoc3("Wesley Cates");
testDoc3.displayPatientArray();
cout << "\nConstructor 4:\n";
Doctor testDoc4("Baylor Bishop", 3);
testDoc4.displayPatientArray();
#endif
Doctor::Doctor() : doctorName("need a name."), patientArraySize(100), numOfPatient(0) {
//Create a dynamic array for patients below:
//stringPtr_t* pArray;
stringPtr_t* patientArray;
patientArray = new stringPtr_t[patientArraySize];
类:
typedef unsigned short ushort_t;
typedef string* stringPtr_t;
class Doctor {
private:
string doctorName;
stringPtr_t patientArray;
ushort_t patientArraySize;
ushort_t numOfPatient;
public:
Doctor();
Doctor(ushort_t patientArrayCapacity);
Doctor(string docName);
Doctor(string docName, ushort_t patientArrayCapacity);
bool addPatient(string patientName);
void displayPatientArray();
void resizePatientArray(ushort_t newArraySize);
string getDoctorName() const {return doctorName;}
ushort_t getNumOfPatient() const {return numOfPatient;}
ushort_t getArraySize() const {return patientArraySize;}
void setDoctorName(string docName) {doctorName.assign(docName);};
void emptyPatientArray() {numOfPatient = 0;}
Doctor& operator =(const Doctor& docSource);
~Doctor() {delete [] patientArray;}
};
最佳答案
您在构造函数 Doctor::Doctor()
中初始化的数组是一个名为“patientArray”的本地变量,而不是您随后要删除的类变量析构函数。
要解决此问题,请将构造函数更改为:
Doctor::Doctor() : doctorName("需要一个名字。"), patientArraySize(100), numOfPatient(0) {
//为下面的患者创建一个动态数组:
//stringPtr_t* pArray;
//删除这里的局部变量声明:stringPtr_t* patientArray;
//
patientArray = new string[patientArraySize];
关于c++ - 被释放的指针没有分配,但看起来是,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22137881/
我正在尝试执行 vagrant up 但一直遇到此错误: ==> default: IOError: [Errno 13] Permission denied: '/usr/local/lib/pyt
我在容器 div 中有一系列动态创建的不同高度的 div。 Varying text... Varying text... Varying text... Varying text.
通过 cygwin 运行 vagrant up 时遇到以下错误。 stderr: /bin/bash: /home/vagrant/.ansible/tmp/ansible-tmp-14872260
今天要向小伙伴们介绍的是一个能够快速地把数据制作成可视化、交互页面的 Python 框架:Streamlit,分分钟让你的数据动起来! 犹记得我在做机器学习和数据分析方面的毕设时,
我是 vagrant 的新手,正在尝试将第二个磁盘添加到我正在用 vagrant 制作的虚拟机中。 我想出了如何在第一次启动虚拟机时连接磁盘,但是当我关闭机器时 然后再次备份(使用 'vagrant
我是一名优秀的程序员,十分优秀!