简体   繁体   English

C++ 析构函数调用 Class 指针调用另一个 class

[英]C++ Destructor call Class Pointer called in Another class

I am working on a school project where I have to parse information from a string array and then place them into an object (class1) of one class using an array of pointers called in another class (class2).我正在做一个学校项目,我必须从字符串数组中解析信息,然后使用在另一个 class 中调用的指针数组将它们放入一个 class 的 object (class1)中。 I am able to get the information into the class1 object but when I try to remove a single class1 object using the pointer in class 2 either nothing happens, I can empty an array stored in class1 object, or I receive scope errors (cannot delete expression of type class1), depending on how I manipulate my code. I am able to get the information into the class1 object but when I try to remove a single class1 object using the pointer in class 2 either nothing happens, I can empty an array stored in class1 object, or I receive scope errors (cannot delete expression类型 class1),取决于我如何操作我的代码。 Here is a small sample:这是一个小样本:

 class Class1 {
 public:
  //Declare constructors
  Class1 (int num, string word, int array[]);
  Class1 ();

  //Destructor
  ~Class1();
  };

   Class1::~Class1(){}
    //followed by definitions assigning variables using this->num = num; etc...

    //Declare Class2
   class Class2 {
   public:
   int index = 0;
   int numStudent;
   //Constructors
   Class2(numStudent);
   Class2();

   ~Class2();
 
   Class1* arrayPtr[5];
   };
   
     //Class2 Definitions
     Class2::Class2 (int numStudent) {
      this->numStudent = numStudent;
      for (int i = 0; i < numStudent; ++i) {
       arrayPtr[i] = new Class1;
     }
     }

     Class1::Class1(){}
     //
     Class2::~Class2() {
        delete[] arrayPtr;
     }

This way I receive an error on Class2 destructor to "Cannot delete expression of type 'Class1 *[3]'. I tried using delete arrrayPtr[I] in a for loop and received no error, outputted message saying Object Removed but recalling print still printed the Object that was supposed to be removed. Any explanation or tips would be helpful.这样我在 Class2 析构函数上收到错误“无法删除 'Class1 *[3]' 类型的表达式。我尝试在 for 循环中使用 delete arrrayPtr[I] 并且没有收到错误,输出消息说 Object 已删除但仍在召回打印打印了应该删除的 Object。任何解释或提示都会有所帮助。

Mistake 1错误 1

The error means that you're trying to use delete[] on arrayPtr which is an array of size 3 with elements of type Class* .该错误意味着您正在尝试在arrayPtr上使用delete[] ,它是一个大小为 3 且元素类型为Class*的数组 In other words, the operand for the delete[] in your example is an array and not a pointer.换句话说,您的示例中delete[]的操作数是一个数组,而不是一个指针。 That is, the operand to delete or delete [] should be a pointer.也就是说, deletedelete []的操作数应该是一个指针。

Mistake 2错误 2

Secondly, nowhere in your program you've used new[] form so using delete[] is incorrect.其次,在您的程序中没有任何地方使用过new[]形式,因此使用delete[]是不正确的。 That is, since you've used new form, you should be using the delete form instead of delete[] .也就是说,由于您使用了new表单,因此您应该使用delete表单而不是delete[]

The correct way to do this would be to loop through the array using a say for loop and then using delete form on each element.正确的方法是使用 say for循环遍历数组,然后在每个元素上使用delete形式。


Note also that better would be to either use smart pointers or better yet use std::vector so that you don't have to do explicit memory management using new and delete .另请注意,最好使用智能指针或更好地使用std::vector ,这样您就不必使用newdelete进行明确的 memory 管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM