Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

后端 未结 2 516
眼角桃花
眼角桃花 2021-02-06 17:37

This error occurs during run time, and I\'m not sure what\'s causing it - the code looks correct to me.

#include 
#include 

using          


        
2条回答
  •  醉酒成梦
    2021-02-06 17:51

    Your problem is here:

    void Event::set(Room r, const std::string& name) {
        d_room = &r;
        d_name = name;
    }
    

    The &r takes the address of an object whose lifetime ends when the function returns, resulting in undefined behaviour when you later try to access it.

    If you want to use pointers, you need to allocate them dynamically:

    void Event::set(Room* r, const std::string& name) {
        d_room = r;
        d_name = name;
    }
    
    // ...
    for (int i = 0; i < noLect; ++i) {
        Room* r = new Room;
        r->d_noSeat = i + 1;
        r->d_hasProjector != r.d_hasProjector;
        lectures[i].set(r, "CSI2372");
        lectures[i].print();
    }
    // ...
    

    But it doesn't look like you need pointers here, you should be able to have

    Room d_room;
    

    in the Event class.

提交回复
热议问题