编译器错误 C2541 - 'delete':删除:无法删除不是指针的对象

Compiler Error C2541 - 'delete' : delete : cannot delete objects that are not pointers

这是我写的class:

class StaticList
{
   private:
   int      headFree;
   int      headList;
   int      locNew;
   StaticListNode* listNodeArr;

   public:
   StaticList(int numberOfElements);
   ~StaticList();

   void addToStaticList(int ComputerNum);
   int getHeadList();
   int getHeadFree();
   StaticListNode* getListNodeArr();
   void show() const;
};

然后我进行动态分配:

StaticList::StaticList(int numberOfElements)
{
   headFree = 1; //Because the first headFree is 1 (the 0 cell is dummy)
   headList = -1;
   locNew = -1;

   this->listNodeArr = new StaticListNode[numberOfElements];

   for (int i = 0; i < numberOfElements - 1; i++) 
       listNodeArr[i].setNext(i + 1);

   listNodeArr[numberOfElements].setNext(-1);
}

The problem is that when I try to delete the allocation through the d'tor, it gives me the error:

enter image description here

可能是什么问题?我错过了什么?

谢谢!

当你为数组中的 n 个元素分配时,array[n] 是你不能访问的地方。

当您调用 deletedelete[] 时,它想要释放某处不属于它的内存。

所以

listNodeArr[numberOfElements].setNext(-1);

是您的 class StaticListNode 数组中的无效位置 改成

listNodeArr[numberOfElements - 1].setNext(-1);

0numberOfElements

之间正确位置的任何内容