简体   繁体   English

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

[英]C++ Compiler Error 2086 redefinition

I have a .cpp file that must include Console.h . 我有一个.cpp文件,其中必须包含Console.h In the file I'm trying to create a map (used later on for a game). 在文件中,我尝试创建地图(稍后用于游戏)。

Error C2086: 'int nMapArray[15][20]: redefinition 错误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 }
};

I know that I'm only supposed to declare nMapArray once but I'm not sure which one to discard. 我知道我只应该声明一次nMapArray ,但是我不确定要丢弃哪一个。 If I discard int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]; 如果我放弃int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]; then it will generate two errors: LNK2019: unresolved external symbol and LNK1120: unresolved externals 那么它将生成两个错误:LNK2019:未解析的外部符号和LNK1120:未解析的外部

Did a bit of googling but I still can't find the fix so help would be appreciated. 做了一些谷歌搜索,但我仍然找不到修复程序,因此将不胜感激。

EDIT: Ok so following the advice of many to get rid of the first inisialisation. 编辑:好的,所以遵循许多人的建议以摆脱第一次的初始化。 From here I get two errors: 从这里我得到两个错误:

error LNK2019: unresolved external symbol "public: virtual class IConsole & __thiscall Win32Console::Color(unsigned short)" (?Color@Win32Console@@UAEAAVIConsole@@G@Z) referenced in function "void __cdecl DrawTile(int,int)" (?DrawTile@@YAXHH@Z) 错误LNK2019:无法解析的外部符号“公共:虚拟类IConsole和__thiscall Win32Console :: Color(无符号短整数)”(?Color @ Win32Console @@ UAEAAVIConsole @@ G @ Z)在函数“ void __cdecl DrawTile(int,int)”中引用(?DrawTile @@ YAXHH @ Z)

and

error LNK1120: 1 unresolved externals 错误LNK1120:1个未解决的外部

Full code: 完整代码:

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

    }
}

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

In Console.h I haven't put the layout in (because I'm not entirely sure how to do that). Console.h我没有放入布局(因为我不确定如何做到这一点)。

Get rid of the first one, the one without the initialisation. 摆脱第一个,没有初始化的。

That solves the compiler issue, the linker errors are another matter. 解决了编译器问题,链接器错误是另一回事。 The reason they appear is simply because the compilation phase is working once the double declaration is fixed. 它们出现的原因仅是因为一旦确定了双重声明,编译阶段就可以正常工作。

Then I suggest you post another question with more details on the linker problems. 然后,我建议您发布另一个问题,以提供有关链接器问题的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM