2015-12-06 168 views
1
void Image::setValues(int start_row, int end_row, int start_column, int end_column, Image & source) 
{ 
    int startPosX = start_row; 
    int startPosY = start_column; 

    for (int row = 0; startPosX < end_row; row++, startPosX++) 
     for (int col = 0; startPosY < end_column; col++, startPosY++) 
      std::cout << startPosX << " " << startPosY << std::endl; 
} 

我叫喜歡For循環函數的參數錯誤

imageTest.setValues(0, 32, 0, 32, imageTest2); 

這個功能我希望的1024套數字(32 * 32)的輸出,而不是我得到的輸出,如下圖所示。

Output

我到底做錯了什麼?

回答

1

您忘記在內部循環的開始初始化startPosY

試試這個:

void Image::setValues(int start_row, int end_row, int start_column, int end_column, Image & source) 
{ 
    int startPosX = start_row; 
    int startPosY = start_column; 

    for (int row = 0; startPosX < end_row; row++, startPosX++) { 
     startPosY = start_column; 
     for (int col = 0; startPosY < end_column; col++, startPosY++) 
      std::cout << startPosX << " " << startPosY << std::endl; 
    } 
} 
+0

@MickCAT謝謝你,什麼是愚蠢的錯誤,會標記爲答案 –

1

startPosXstartPosY沒有做什麼有用的東西上面,除了使邏輯更加複雜,導致你犯錯誤。

這是更簡單,更清晰,沒有他們:

for (int row = start_row; row < end_row; row++) 
    for (int col = start_column; col < end_column; col++) 
     std::cout << row << " " << col << std::endl; 
+0

感謝你爲這個他們是後來的東西 –