c++ - getting error segmentation fault (core dumped) process returned 139 (0x8B) -


i have list of pointer of same class inside class. when want access data, error.what should resolve problem. newbie c++ , can not figure out problem.

#include <iostream> #include <list> using namespace std; class alpha {     public:      alpha() {}      alpha(int val) : i(val) {}      virtual ~alpha() {}      void addtarget(alpha* alpha)     {         targets.push_back(alpha);     }      void display() {         cout << << " -------------";     }      private:     int i; //!< member variable "i"     list<alpha*> targets;    }; 

and here main function:

  #include <iostream>   #include <list>   #include "alpha.h"   using namespace std;    int main()   {     list<alpha> teama, teamb;      alpha* alptr;     for(int = 0; < 3; i++)     {     alptr = new alpha;     teama.push_back(*alptr);     alptr = nullptr;     }     for(int = 0; < 3; i++)    {     alptr = new alpha;     teamb.push_back(*alptr);     alptr = nullptr;    }     list<alpha>::iterator = teama.begin();     for(;it != teama.end(); it++)    {     for(list<alpha>::iterator itr = teamb.begin();                                    itr != teamb.end();itr++)     {         it->addtarget(&(*itr));     } } = teama.begin(); list<alpha*>::iterator itr = it->gettargets().begin(); /// trying access while(itr != it->gettargets().end()) {     (*itr)->display();     itr++; }  return 0; } 

and here output:

segmentation fault (core dumped) process returned 139 (0x8b) 

does expect?

it = teama.begin(); list<alpha*> targets = it->gettargets(); // local targets list copy!  list<alpha*>::iterator itr = targets.begin();  /// trying access while (itr != targets.end()) {     (*itr)->display();     itr++; } 

the problem in code:

list<alpha*>::iterator itr = it->gettargets().begin(); //                                      ^       ^ //                                      |       | // returns temporary list copy -------+       | // returns iterator temp. list copy --+ 

now temporary list destroyed.

    (*itr)->display();     itr++; //   ^ //   | //   +--- error: iterator list has been destroyed 

Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -