Omnet++ A cRuntimeError exception is about to be thrown std::length_error: basic_string::_M_create

Omnet++ A cRuntimeError exception is about to be thrown std::length_error: basic_string::_M_create

所以当我 运行 我的代码

时出现这个错误

Error in module (TraCIDemoRSU11p) RSUExampleScenario.rsu[0].appl (id=8) at event #4000, t=45.40151998544: std::length_error: basic_string::_M_create. TRAPPING on the exception above, due to a debug-on-errors=true configuration option. Is your debugger ready?

说明我在循环多次后在这一行出现了这个问题

 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 和 veins 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;
}

}

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

所以我将 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 循环不同,那就太好了。 ^^