Conversion from 'myItem*' to non-scalar type 'myItem' requested

前端 未结 4 788
感动是毒
感动是毒 2020-12-05 17:45

I have this C++ code:

#include 
using namespace std;
struct MyItem
{
  int value;
  MyItem* nextItem;
};

int main() {
    MyItem item = new          


        
4条回答
  •  不思量自难忘°
    2020-12-05 18:36

    You've mixed

    MyItem item;
    

    which allocates an instance of MyItem on the stack. The memory for the instance is automatically freed at the end of the enclosing scope

    and

    MyItem * item = new MyItem;
    

    which allocates an instance of MyItem on the heap. You would refer to this instance using a pointer and would be required to explicitly free the memory when finished using delete item.

提交回复
热议问题