警告如下:
/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:48:5: warning:
this ‘if’ clause does not guard... [-Wmisleading-indentation]
if (toHexSize < 1)
^~
/home/dronz/OF/apps/myApps/HexMap/src/HexMap.cpp:51:2: note: ...
this statement, but the latter is misleadingly indented as
if it were guarded by the ‘if’
MapTileSizeAtZoom = toHexSize;
^~~~~~~~~~~~~~~~~
代码如下:
if (toHexSize < 1)
toHexSize = 1;
MapTileSizeAtZoom = toHexSize;
如果 MapTileSizeAtZoom ...
那一行的缩进更深,我认为可能会有误导性,但是它与 'if' 在同一层级缩进,所以对我来说看起来是正确的。
我想也许存在额外的空格和/或制表符,但是我删除了文本后面的任何不必要的空白字符,但没有什么改变。
我想也许是由于空行的原因导致警告,但是将其删除并没有停止警告。
此外,在同一个 .cpp 文件中,在它之前有这段代码,但它没有发出警告:
if (toHexSize < 1)
toHexSize = 1;
HexInfo centerOnHex;
if (SelectedHex.type != -1)
为什么它会警告其中一个(总之),却不警告另一个,不管这是否是GCC的错误,我该怎么避免它呢?
代码
#include "HexMap.h"
#include <algorithm>
#include <cmath>
//--------------------------------------------------------------
HexMap::HexMap()
{}
//--------------------------------------------------------------
int HexMap::SetZoom(int toHexSize)
{
if (toHexSize < 1)
toHexSize = 1;
HexInfo centerOnHex;
if (SelectedHex.type != -1)
{
// Center map on the selected hex.
centerOnHex = SelectedHex;
}
else
{
// Center map on current center of viewpoint.
centerOnHex = GetHex(
MapFrame.x + MapFrame.getWidth() / 2,
MapFrame.y + MapFrame.getHeight() / 2 );
if ((centerOnHex.x > WORLDMAPWIDTH) || (centerOnHex.x < 0))
centerOnHex.x = WORLDMAPWIDTH / 2;
if ((centerOnHex.y > WORLDMAPHEIGHT) || (centerOnHex.y < 0))
centerOnHex.y = WORLDMAPHEIGHT / 2;
}
setHexDisplaySize(toHexSize);
// Center map:
HexOriginX = MapFrame.x + MapTileWidth * 0.25f;
HexOriginY = MapFrame.y + MapTileHeight * 0.5f;
ViewPosOnWorld.set(
centerOnHex.x - (MapFrame.getWidth() / 2) / MapTileWidth,
centerOnHex.y - (MapFrame.getHeight() / 2) / MapTileHeight);
return 0;
}
//--------------------------------------------------------------
void HexMap::setHexDisplaySize(int toHexSize)
{
if (toHexSize < 1)
toHexSize = 1;
MapTileSizeAtZoom = toHexSize;
MapTileWidth = MapTileSizeAtZoom * 1.5f; // hex x-spacing is 1.5 * r
MapTileHeight = MapTileSizeAtZoom * 1.73205f; // hex height = sqrt(3*r)
// Size images & hexmask:
MaskWidth = MapTileHeight * 1.154700538; // 1/(sqrt(3)/2)
}