Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) Error

后端 未结 2 514
眼角桃花
眼角桃花 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:40

    Issue

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

    You are referencing to the temporary object: Room r passed by value, which is destroyed at the end of the scope: }.

    Instead you must reallocate the member pointer:

    d_room = new Room(r);
    

    Why it went wrong

    Because you are writing C-style code in C++ classes.

    In C++ we tend to:

    1. Avoid naked pointers, prefer smart pointers:

      class Event 
      {
          std::shared_ptr d_room;
       ...
      
      Event::~Event() { /* no need to delete */ }
      
    2. Use constructor overloading (instead of using set-like functions after construction):

      Event(Room& r, const std::string& name):
              d_room(new Room(r)),
              d_name(name)
      {}
      
    3. Pass by reference:

      void set(Room& r, const std::string& name);
      
    4. Avoid raw arrays, use STL facilities instead:

      std::vector lectures;
      // or
      std::array lectures;
      

    Another issue

    r.d_hasProjector != r.d_hasProjector; // checks if r.d_hasProject is not itself

    You probably want

    r.d_hasProjector = !r.d_hasProjector;

    Complete code: link

    Also, here is a must-read link about advanced C++ stuff which, I believe, will be very useful to you: http://www.parashift.com/c++-faq/

    Edit: I forgot about your question:

    In addition to this, the printed text displays numbers that are very large and often negative. What is causing this?

    Those numbers are garbage. Variables that are not explicitly initialized are not initialized at all. Memory is allocated but holds old information from previous program. It could contain anything. When you read from uninitialized variables, you'll get this garbage. You had a pointer which was pointing to a destroyed object. So the pointer was effectively uninitialized.

提交回复
热议问题