2015-11-28 103 views
0

我的編譯器告訴我,我有一個錯誤,但我已經通過電子郵件發送了我的教師,他說我的代碼非常好。錯誤C2440,Battleship C++

的錯誤是錯誤:

1 error C4716: 'ShipPlacement' : must return a value, line 139

我不確定到哪裏出了錯正是因此我想分享我的代碼爲ShipPlacement

ShipPlacement(Coord grid[10][10]) 
{ 
    CoordAndBearing SetBowAndDirection(); 

    CoordAndBearing cab; 
    cab = SetBowAndDirection(); 
    int start; 

    if((cab.dir == 3) || (cab.dir == 1)) // GOING HORIZONTAL // 
    { 
     if (cab.dir == 3) 
      start = cab.bx; 
     else 
      start = cab.bx - 4; 

     for(int i = start; i <= start + 4; i = i + 1) 
     { 
      grid[i][cab.by].isShip = true; 
     } 
    } 
    else      // GOING VERTICAL 
    { 
     if(cab.dir == 0) 
      start = cab.by; 
     else 
      start = cab.by - 4; 
     for (int i = start; i <=start + 4; i = i + 1) 
     { 
      grid[cab.bx][i].isShip = true; 
     } 
    } 
} 

,這裏是我的int main

int main() 
{ 
    srand((unsigned int) time (NULL)); 

    void ShipPlacement(Coord grid[10][10]); 
    Coord grid[10][10]; 

    SetGridParameters(grid); 

    ShipPlacement(grid); 

    int ammo = 18; 
    int hits = 0; 
    while (hits < 5 && ammo >0) 
    { 
     int x; 
     int y; 

     DisplayGrid(grid); 
     cout << "Ammo left = " << ammo << endl; 
     cout << "Enter Coord: " << endl; 
     cin >> x >> y; 
     ammo= ammo - 1; 

     if (grid [x][y].isShip == true) 
     { 
      hits = hits + 1; 
     } 
     else 
     { 
      cout << " You missed... " << endl; 
     } 
    } 
    DisplayGrid(grid); 
    if(hits == 5) 
    { 
     cout << "You sunk the U.S.S McCall!!"; 
    } 
    else 
    { 
     cout << " You lost "; 
    } 


    system("pause"); 
    return 0; 
} 
+1

您錯過了爲您的'ShipPlacement()'函數指定'void'返回類型。在這種情況下,一些編譯器會假設爲「int」,因此會引起投訴。 –

+1

這些日子誰指導老師? –

回答

2

您已經定義了ShipPlacement功能,無需返回類型。一些(大多數?)編譯器會發出警告,聲明他們假設它返回一個int,然後是錯誤,因爲它沒有。

只是明確地定義它爲「返回」void(即,void ShipPlacement(Coord grid[10][10])),你應該沒問題。

相關問題