错误C2440,Battleship C ++

时间:2015-11-28 09:56:18

标签: c++ syntax-error

我的编译器告诉我我有错误,但我已通过电子邮件发送给我的导师,他说我的代码完全正常。

错误是错误:

  

1错误C4716:'ShipPlacement':必须返回第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 个答案:

答案 0 :(得分:2)

您已定义ShipPlacement函数而没有返回类型。一些(大多数?)编译器会发出警告,说明他们假设它返回int,然后是错误,因为它没有。

只需将其明确定义为&#34;返回&#34; void(即void ShipPlacement(Coord grid[10][10])),你应该没事。