Assignment of string to structure element

后端 未结 5 2044
长情又很酷
长情又很酷 2021-01-22 22:50

My program crashes when I try to assign a string value to a member of a structure. My suspicion is that the member (of type string) within the structure was never properly alloc

5条回答
  •  难免孤独
    2021-01-22 23:25

    The way you are allocating node is incorrect: if you want to dynamically allocate non-POD types in C++, you need to use new, since it will call the required constructors (followed by a call to delete when appropriate).

    But it might be simpler to allocate an automatic instance:

    DataRow node;
    

    If you do need a pointer, make sure to have a look at smart pointers, particularly std::unique_ptr and std::shared_ptr. See also boost::scoped_ptr.

提交回复
热议问题