gpt4 book ai didi

C++ 编译器错误 2086 重新定义

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

我有一个 .cpp必须包含 Console.h 的文件.在文件中,我正在尝试创建 map (稍后用于游戏)。

错误 C2086:'int nMapArray[15][20]:重新定义

#include "Console.h"
#include <Windows.h>
#include <stdint.h>

// Map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 15


// Tile Types
#define TILE_FLOOR 0
#define TILE_WALL 1


// Map declaration
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ];

// Map Layout
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]=
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

我知道我只应该声明 nMapArray一次,但我不确定要丢弃哪一个。如果我丢弃 int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ];那么它将产生两个错误:
LNK2019:未解析的外部符号

LNK1120: Unresolved external 因素

做了一些谷歌搜索,但我仍然找不到修复程序,因此将不胜感激。

编辑:
好的,所以遵循许多人的建议来摆脱第一次初始化。从这里我得到两个错误:

错误 LNK2019:函数“void __cdecl DrawTile(int,int)”中引用了未解析的外部符号“公共(public):虚拟类 IConsole & __thiscall Win32Console::Color(unsigned short)”(?Color@Win32Console@@UAEAAVIConsole@@G@Z) (?DrawTile@@YAXHH@Z)



错误 LNK1120:1 Unresolved external

完整代码:
#include "Console.h"
#include <Windows.h>
#include <stdint.h>

// Map dimensions
#define MAP_WIDTH 20
#define MAP_HEIGHT 15


// Tile Types
#define TILE_FLOOR 0
#define TILE_WALL 1

// Map Layout
int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]=
{
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
};

void DrawMap( void );
bool IsPassable( int x, int y );
void DrawTile( int x, int y );

int main( void )
{
console.SetTitle( "Article Two Demo" );

// Declare the player's position
int nPlayerX = 4;
int nPlayerY = 4;

// Main game loop
while( true )
{
// Draw the map
DrawMap();

// Draw the player to the screen
console.Color( RED );
console.Position( nPlayerX, nPlayerY );
console << '@';

// Input phase - Wait for the player to do something
KEYPRESS sKeyPress = console.WaitForKeypress();

// Process the input
switch( sKeyPress.eCode )
{
// Move up
case CONSOLE_KEY_UP:
// Can we move to the tile above?
if( IsPassable(nPlayerX, nPlayerY-1) )
{
// Move up
nPlayerY--;
}
break;

// Move left
case CONSOLE_KEY_LEFT:
// Can we move to the tile to the left of the player?
if( IsPassable(nPlayerX-1, nPlayerY) )
{
// Move left
nPlayerX--;
}
break;

// Move right
case CONSOLE_KEY_RIGHT:
// Can we move to the tile to the right of the player
if( IsPassable(nPlayerX+1, nPlayerY ) )
{
// Move right
nPlayerX++;
}
break;

// Move down
case CONSOLE_KEY_DOWN:
// Can we move to the tile below the player?
if( IsPassable(nPlayerX, nPlayerY+1) )
{
// Move down
nPlayerY++;
}
break;

// Escape key
case CONSOLE_KEY_ESCAPE:
// Quit the program
return 0;

// Ignore any other keys
default:
break;
}
}

// If execution gets here, the program is done
return 0;
}

// IsPassable Function ///////////////////////////////////////////////////////////////////
//
// This function analyzes the coordinates of the map array specified and returns
// true if the coordinate is passable (able for the player to occupy), false if not.
//
bool IsPassable( int x, int y )
{
// Before we do anything, make darn sure that the coordinates are valid
if( x < 0 || x >= MAP_WIDTH || y < 0 || y >= MAP_HEIGHT )
return false;

// Store the value of the tile specified
int nTileValue = nMapArray[y][x];

// Return true if it's passable
if( nTileValue == TILE_FLOOR)
return true;

return false;
}

// DrawMap Function //////////////////////////////////////////////////////////////////////
//
// This function draws the entire map to the screen.
//
void DrawMap( void )
{
for( int y = 0; y < MAP_HEIGHT; y++ )
{
for( int x = 0; x < MAP_WIDTH; x++ )
{
DrawTile(x, y);
}
}
}

// DrawTile Function /////////////////////////////////////////////////////////////////////
//
// Draws a map tile for the map coordinates specified.
//
void DrawTile( int x, int y )
{
console.Position( x, y );
switch( nMapArray[y][x] )
{
case TILE_FLOOR:
console.Color( GREY );
console << '.';
break;

case TILE_WALL:
console.Color( GREY );
console << '#';
break;

}
}

//////////////////////////////////////////////////////////////////////////////////////////

Console.h我还没有把布局放进去(因为我不完全确定怎么做)。

最佳答案

摆脱第一个,没有初始化的那个。

这解决了编译器问题,链接器错误是另一回事。它们出现的原因仅仅是因为一旦修复了双重声明,编译阶段就开始工作了。

然后我建议您发布另一个问题,其中包含有关链接器问题的更多详细信息。

关于C++ 编译器错误 2086 重新定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19702744/

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