Question

Hi I ran into an error a couple of days back. The code is not the same but is similar to what is listed below:

     struct AB{

          vector<int> * temp;

          AB(){
              temp = new vector<int>;

          }

          AB(const AB &other){

                     temp = new vector<int> 
                    //and I am memberwise copying other.temp to temp. (Not shown here)
          }
         ~AB(){
                      delete AB;
          }
      };    

And in the main class I am doing this

      unordered_map<int, AB> mapOfAB;
      mapOfAB[0].temp->push_back(1);

This is giving me a segmentation fault, but if I am making temp as a stack(non-dynamic) variable, it runs fine. I hope I am specific enough.

Thanks in advance

Was it helpful?

Solution

You have a raw pointer, and you do not have an assignment operator. You have violated the Rule of Three.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top