2012-07-31 155 views
0

我有下面的代碼片段中,我想打印一些語句到一個XML文件中:For循環沒有得到執行

void parseXML::writeStruct(std::fstream& abc,std::string prnt) 
{ 
    for (map<string,struct structSet>::iterator it = structData.begin();it != structData.end();it++) 
    { 
     if (((it->second.parent.compare("")==0) && (it->second.written == false))) 
     { 
     bool write = true; 
     if (it->second.type.compare("")==0) 
     { 
      for (set<std::string>::iterator i = it->second.fields.begin(); i != it->second.fields.end(); i++) 
      { 
       map<string,struct fieldSet>::iterator fd = fieldData.find(*i); 
       if (fd != fieldData.end()) 
       { 
        std::string type = fd->second.type; 
        map<string,struct structSet>::iterator ntC = structData.find(type); 
        if (ntC != structData.end()) 
        { 
         if (ntC->second.type.compare("") != 0) 
         { 
          map<string,struct structSet>::iterator ntC = structData.find(ntC->second.type); 
          if (ntC == structData.end()|| ntC->second.type.compare("")!= 0||ntC->second.written == false) 
          { 
           continue; 
          } 

         } 
         else 
         { 
          map<string,struct structSet>::iterator ntC = structData.find(ntC->second.type); 
          if (ntC->second.parent.compare(it->second.name)) 
          { 
          } 
          else if (ntC->second.written == true) 
          { 
           abc << INDENT << "\t" <<"\t" << "<nonterminal ref= \"" << ntC->second.name.c_str() << "\">" << std::endl; 
           abc << INDENT << "\t" << "\t" <<"\t" << "<name>" << fd->second.name.c_str() << "</name>" << std::endl; 
           abc << INDENT << "\t"<< "\t" << "</nonterminal >" << std::endl; 
          } 
         } 
        } 

的問題是,它不執行第一for循環:

for (map<string,struct structSet>::iterator it = structData.begin(); it != structData.end(); it++) 

這有什麼可能的原因?

+7

唯一可能的原因是:'structData '是空的。 – 2012-07-31 22:30:26

+1

你的縮進遍佈整個地方。如果可能,清理它。 – Wug 2012-07-31 22:31:05

+2

我可以推薦一些重構嗎? [箭頭代碼](http://www.codinghorror.com/blog/2006/01/flattening-arrow-code.html)很難理解,難以調試和難以維護。 – 2012-07-31 22:31:14

回答

2

這裏是一個(希望)完整列表,比較遺憾的是明顯的項目,但我們不知道如何經歷你:

  • structData是空
  • structData被破壞,其中的應用程序崩潰
  • parseXML::writeStruct從未執行
  • for循環得到執行,但以下(iffor)條件失敗,並且您錯誤地解釋了此錯誤。

選擇調試器或添加跟蹤消息(不要忘記使用endl,因爲輸出通常是線路緩衝的,並且在發生崩潰時將丟失)。

注:後只有代碼的相關部分,它的其餘部分是我們剛剛噪聲(除非你想有一個代碼審查其中有https://codereview.stackexchange.com/