Omnet ++即将引发std :: length_error:basic_string :: __ M_create cRuntimeError异常

时间:2018-09-20 09:41:22

标签: c++ omnet++ veins sumo

所以我在运行代码时会收到此错误

  

模块(TraCIDemoRSU11p)中的错误RSUExampleScenario.rsu [0] .appl(id = 8)在事件#4000,t = 45.40151998544:std :: length_error:basic_string :: _ M_create。   由于出现debug-on-errors = true配置选项,因此会在上述异常上进行TRAPPING。调试器准备好了吗?

它表明我在循环多次后在此行遇到了这个问题

 VehTD = *iteh2;

请告知,我正在尝试从此车辆列表中删除至另一个车辆列表,并删除移动的项目。

 std::list<std::pair<std::string,std::string>>::const_iterator iteh2 = waitingList.begin();
      for (std::list<std::pair<std::string,std::string>>::const_iterator iteh = waitingList.begin(); iteh != waitingList.end() && !waitingList.empty(); ){
          iteh2 = iteh;
          cout<<"Veh ID TF 1:";
          cout<<VehTD.first<<endl;
          cout<<VehTD.second<<endl;
          VehTD = *iteh2;
          cout<<"Veh ID TF 2:";
          cout<<VehTD.first<<endl;
          cout<<VehTD.second<<endl;
          CompareResult3 = Locks(Locked,VehTD.second);
          if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
           //remove from waiting and add to crossing
              std::string ehk = "";
              simtime_t VehicleT = simTime();
              std::pair<std::pair<std::string, std::string>, simtime_t> VehicleWithTime;
              crossingList.push_back(VehTD);


              iteh2 = waitingList.erase(iteh2);
              }
              else{
                  ++iteh;
              }
       }
   }

我正在使用Oment ++ 5.0和静脉4.4

我尝试使用此函数,似乎异常发生在列表的末尾。

更新: 我有这个输出:

WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
WCounter 3
Moving from waiting list Timer Function Exception
WCounter 1
WCounter 2
Moving from waiting list Timer Function Exception

WCounter是一个变量,用于显示在前面所述的异常发生之前,它循环了多少次。

根据请求,我添加了锁功能:

  std::pair<std::list<std::string>,int> TraCIDemoRSU11p::Locks(std::list<std::string> alreadyLocked, std::string laneNo){
    bool debugL;
    //create variables for the lanes for easier use
try{
      debugL = false;
      zero = "171270266#0_0";
      one = "171270266#0_1";
      two = "-171270025#1_0";
      three = "-171270025#1_1";
      four = "-171270266#1_0";
      five = "-171270266#1_1";
      six = "171270025#0_0";
      seven = "171270025#0_1";



      //create lists of locks

      Locks0 = Locks1 = Locks2 = Locks3 = Locks4 = Locks5 = Locks6 = Locks7 = RequestToLock = {};
      CounterOfSimilarLanes = 0;

      //set of 0 ,Locks0;
      Locks0.push_back(zero);
      Locks0.push_back(five);
      Locks0.push_back(seven);
      //set of 1 ,Locks1;
      Locks1.push_back(one);
      Locks1.push_back(three);
      Locks1.push_back(six);
      //set of 2 ,Locks2;
      Locks2.push_back(one);
      Locks2.push_back(two);
      Locks2.push_back(seven);
      //set of 3 ,Locks3;
      Locks3.push_back(zero);
      Locks3.push_back(three);
      Locks3.push_back(five);
      //set of 4 ,Locks4;
      Locks4.push_back(one);
      Locks4.push_back(three);
      Locks4.push_back(four);
      //set of 5 ,Locks5;
      Locks5.push_back(two);
      Locks5.push_back(five);
      Locks5.push_back(seven);
      //set of 6 ,Locks6;
      Locks6.push_back(three);
      Locks6.push_back(five);
      Locks6.push_back(six);
      //set of 7 ,Locks7;
      Locks7.push_back(one);
      Locks7.push_back(four);
      Locks7.push_back(seven);
      //This is the request to lock from the vehicle using its lane number
      if (laneNo == zero ){
          RequestToLock.insert(RequestToLock.end(),Locks0.begin(),Locks0.end());
      }else if (laneNo == one){
          RequestToLock.insert(RequestToLock.end(),Locks1.begin(),Locks1.end()) ;
      }else if (laneNo == two){
          RequestToLock.insert(RequestToLock.end(),Locks2.begin(),Locks2.end()) ;
      }else if (laneNo == three){
          RequestToLock.insert(RequestToLock.end(),Locks3.begin(),Locks3.end()) ;
      }else if (laneNo == four){
          RequestToLock.insert(RequestToLock.end(),Locks4.begin(),Locks4.end()) ;
      }else if (laneNo == five){
          RequestToLock.insert(RequestToLock.end(),Locks5.begin(),Locks5.end()) ;
      }else if (laneNo == six){
          RequestToLock.insert(RequestToLock.end(),Locks6.begin(),Locks6.end()) ;
      }else if (laneNo == seven){
          RequestToLock.insert(RequestToLock.end(),Locks7.begin(),Locks7.end()) ;
      }

}   //if the already locked from the controller is empty so we take the requested lock
catch(const std::exception &e){
    cout<<"Preparing Locks Algorithm Exception"<<endl;
}
try{
    if (alreadyLocked.empty()){

          markOfLocks = 0;
          alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
          Locked.insert(Locked.end(),RequestToLock.begin(),RequestToLock.end()) ;
          if (debugL == true){
              cout<<"First Lock in the locking algorithm"<<endl;
            //  dumplistLock(Locked);
          }
          ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);

      }else{ // if the already locked by the controller isnt empty, so we need to compare the request with it
          //Search for similar items in the 2 lists
          for(std::list<std::string>::/*const_*/iterator it_1 = alreadyLocked.begin();it_1 != alreadyLocked.end();)
          {
              for(std::list<std::string>::/*const_*/iterator it_2 = RequestToLock.begin(); it_2 != RequestToLock.end();)
              {
                  if(*it_1 == *it_2)
                  {
                      ++CounterOfSimilarLanes;
                  }

                  ++it_2;

              }
                  ++it_1;
          }


          if (RequestToLock == alreadyLocked){
              markOfLocks = 1;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"Same Lane"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);

          }else if(CounterOfSimilarLanes == 0){ //if the request is concurrent meaning totally different locks
              markOfLocks = 2;
              //alreadyLocked.insert(alreadyLocked.end(),RequestToLock.begin(),RequestToLock.end()) ;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"Concurrent Lane"<<endl;
                  dumplistLock(Locked);
              }

          }else if(CounterOfSimilarLanes == 1  ||  CounterOfSimilarLanes == 2){ // if there's a similarity is 1 lock at least so it leads to conflict
              markOfLocks = 3;
              Locked.clear();
              Locked = {};
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<< "opposite Lane"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
          }else{
              markOfLocks = 1;
              Locked.clear();
              Locked.insert(Locked.end(),alreadyLocked.begin(),alreadyLocked.end()) ;
              if (debugL == true){
                  cout<<"default case"<<endl;
                  dumplistLock(Locked);
              }
              ReturnOfLocks = make_pair(alreadyLocked,markOfLocks);
         }

      }
}catch(const std::exception &e){
    cout<<"Lock Cases Exceptions"<<endl;
}

try{
      return ReturnOfLocks;
}catch(const std::exception &e){
    cout<<"No Return of Locks in Locks Alg Exception"<<endl;
}

}

2 个答案:

答案 0 :(得分:0)

您应该使用iterator而不是const_iterator,因为您要修改(即删除)指定的元素。
顺便说一下,在代码中iteh2是不必要的-iteh就足够了,并且VehicleTehk变量没有使用并且可以删除。

答案 1 :(得分:0)

所以我将for循环更改为while循环,并且没有显示异常,也不确定原因。

因此循环现在看起来像这样:

     //while loop instead.
   std::list<std::pair<std::string,std::string>>::iterator iterwil = waitingList.begin();
   std::pair<std::string, std::string> Vehwil ;
   while (iterwil != waitingList.end()){
       Vehwil = *iterwil;
       CompareResult3 = Locks(Locked,Vehwil.second);
       if(CompareResult3.second == 1 || CompareResult3.second == 2 ){
           crossingList.push_back(Vehwil);
           waitingList.erase(iterwil++);
       }else{
           ++iterwil;
       }

   }

如果有人有正当的理由启发我,为什么while循环毫无例外地工作,不像for循环那么好。 ^^“