2012-07-31 145 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/