2017-04-25 68 views
1

我的問題在於,我的基本情況永遠不會執行,因爲循環,這是運行應用程序的主要組件沒有執行。我正在考慮創建一個嵌入式for循環,但我想不出讓for循環從最大向量位置向最小向量位置擺動的方法。此外,要注意觀察前V3是一個很小的事情同時包含對象A和B,因爲對象C是A和B振盪爲循環遞歸行動

MCVE版本的超:http://coliru.stacked-crooked.com/a/c6fdc017118e98f2

void Obj::objhelper() 
    { 

     recurisivehelper(); 


     if ((v1[0].getx() == 0 && v2[0].getx() > 0) || 
      (v1[0].getx() > 0 && v2[0].getx() == 0)) 
     { 
      if (v1[0].getx() == 0 && v2[0].getx() > 0) 
      { 
       print(3); 
       return; 
      }else 
      { 
       print(4); 
       return; 
      } 
     } 

     else if(v1[0].getx() > 0 && v2[0].getx() > 0) 
     { 
      objhelper(); 
     } 

     return; 
    } 

//This method is not touched 
//This is a recursive helper method of the helper method of the main method 
    void Obj::recursivehelper() 
    { 

     for (int i = 0; i < 2; i++) { 

      if (v3[i].gety() == "str1" || 
       v3[i].gety() == "str2" || 
       v3[i].gety() == "str3") 
      { 
       int temp = 0; 

       Obj1 obj(v1,:v2); 
       obj.display(); 

       v3[i].doa(v3[i + 1]); 

       obj.display(); 
       v2[0]--; 
      }else if (v3[i].gety() == "str4" || 
         v3[i].gety() == "str5" || 
         v3[i].gety() == "str6" ) 
      { 

       Obj1 obj(v1,sv2); 
       obj.display(); 

       v3[i].doa(v3[i + 1]); 


       obj.display(); 
       v1[0]--; 
      } 
     } 

     return; 
    } 
+1

如果您發佈了[MCVE](https://stackoverflow.com/help/mcve)例如在http://ideone.com/或http://coliru.stacked-crooked.com/與一個可運行的主,這個問題將是非常有吸引力的解決。 – javaLover

+0

這是C++中的一個MCVE版本http://coliru.stacked-crooked.com/a/a268bd6b5618a768 – KRYMauL

+0

不錯,它好得多。然而,它不是可編譯的,所以它仍然不是MCVE,只是這個問題是關於編譯器錯誤的問題。 ...我想有人正在盯着這個問題,一旦你提供了MCVE,你會馬上得到答案。 – javaLover

回答

0

傳遞參數by value不同於passing by reference

這裏是問題的一部分: -

void recursivehelper(std::vector<int>v1, std::vector<int>v2, std::vector<std::string>v3) { 
    .... 
     v1[0]--; //<-- modify internal copy 
     v2[0]--; 
    ... 
    return; 
} 

v1[0]--;v2[0]--;修改副本v1v2,實際上什麼也不做,從主叫效果v1v2recursivefunc())的角度。

使其按預期工作的簡單方法之一是通過引用。

void recursivefunc(std::vector<int>, std::vector<int>, std::vector<std::string>); 
void recursivehelper(std::vector<int>, std::vector<int>, std::vector<std::string>); 

更改都聲明和實現的簽名

void recursivefunc(std::vector<int>&, std::vector<int>&, std::vector<std::string>&); 
void recursivehelper(std::vector<int>&, std::vector<int>&, std::vector<std::string>&); 

這裏是demo