Компилятор C ++ Ошибка 2086 Переопределение

0

У меня есть .cpp файл, который должен включать Console.h. В файле я пытаюсь создать карту (используемую позже для игры).

Ошибка 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: нерешенные внешние

Немного искал, но я все еще не могу найти исправление, поэтому помощь будет оценена.

EDIT: Хорошо, следуя советам многих, чтобы избавиться от первой inisialisation. Отсюда я получаю две ошибки:

ошибка LNK2019: неразрешенный внешний символ "public: виртуальный класс IConsole & __thiscall Win32Console :: цвет (без знака short)" (? Color @Win32Console @@UAEAAVIConsole @@G @Z), на который ссылается функция "void __cdecl DrawTile (int, int)" (? DrawTile @@YAXHH @Z)

а также

ошибка LNK1120: 1 неразрешенные внешние

Полный код:

#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 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 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 я не помещал макет (потому что я не совсем уверен, как это сделать).

  • 0
    Если вы, без сомнения, отнимаете много времени, чтобы заявить, что расширение можно выбросить из окна, удалите первое. Это в шапке? если это так, тогда extern это. Если нет, избавьтесь от этого.
  • 0
    1. Какое (релевантное) содержимое Console.h ? Что LNK2019 вы получаете, когда удаляете int nMapArray[ MAP_HEIGHT ][ MAP_WIDTH ]; (который вы должны удалить)?
Показать ещё 2 комментария
Теги:
compiler-errors

1 ответ

0

Избавьтесь от первого, без инициализации.

Это решает проблему компилятора, а ошибки компоновщика - другое дело. Причина, по которой они появляются, - это просто потому, что фаза компиляции работает, когда фиксируется двойное объявление.

Затем я предлагаю вам задать другой вопрос с более подробной информацией о проблемах с компоновщиками.

Ещё вопросы

Сообщество Overcoder
Наверх
Меню