gpt4 book ai didi

c++ - 错误 C2440,战舰 C++

转载 作者:行者123 更新时间:2023-11-28 02:16:16 25 4
gpt4 key购买 nike

我的编译器告诉我有错误,但我已经给我的导师发了电子邮件,他说我的代码非常好。

错误是错误:

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;
}

最佳答案

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

只需将其明确定义为“返回”void(即 void ShipPlacement(Coord grid[10][10])),您应该没问题。

关于c++ - 错误 C2440,战舰 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33970006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com