构建c ++程序时出现多个错误

时间:2016-01-14 03:57:18

标签: c++ build compiler-errors

在我的节目中,我正在构建一个迷宫。每当我构建我的项目时,我都会收到这些错误。我知道这些错误是指我的头文件中的第1-3行。但是我已经将这些行放在我的程序中的不同位置,我无法构建我的项目。在我的头文件中,我定义了一个迷宫类和一个坐标类。我知道我的程序尚未完成,但我需要修复这些错误,以便我可以运行并测试我的程序。

部首:

const int HEIGHT = 1003;
const int WIDTH = 1003;
char grid[WIDTH][HEIGHT];

class Maze
{
    public:
        Maze();
        ~Maze();
        void drawMaze();
        void addPaths();
        void startGame();
        void movePlayer(int);
        bool solved();
        int getKey();
        void addDestinationToGrid();
    private:

};

class Coordinate
{
    public:
        int row, column;
};

Coordinate player;

const static char PLAYER = 'P';
const static char DESTINATION = 'X';
const static char START = 'S';
const static char PATH = ' ';
const static char WALL = (char) 219;

实现:

#include "maze.h"
#include "windows.h"
#include <stack>
using namespace std;


Maze::Maze()
{

}

Maze::~Maze()
{

}

void Maze::drawMaze()
{
    for (int x = 0; x < HEIGHT; x++)
    {
        for (int y = 0; y < WIDTH; y++)
        {
            grid[x][y] = WALL;
        }
    }
}

void Maze::startGame()
{
    int input;
    do
    {
            input = getKey();
            movePlayer(input);
    } while (!solved());
}

bool Maze::solved()
{
    return true;
}

void Maze::movePlayer(int)
{

}

int Maze::getKey()
{
    int result = 0;
    while (!solved() && result == 0)
    {
        short MAX_SHORT = 0x7FFF; //111111111111111
        if (GetAsyncKeyState(VK_LEFT) & MAX_SHORT)
        {
            result = VK_LEFT;
        }
        else if (GetAsyncKeyState(VK_UP) & MAX_SHORT)
        {
            result = VK_UP;
        }
        else if (GetAsyncKeyState(VK_RIGHT) & MAX_SHORT)
        {
            result = VK_RIGHT;
        }
        else if (GetAsyncKeyState(VK_DOWN) & MAX_SHORT)
        {
            result = VK_DOWN;
        }
    }
    return result;
}

void Maze::addPaths()
{
    Coordinate currentLocation;
    Coordinate startLocation;
    Coordinate endLocation;
    std::stack<Coordinate> stack;

    currentLocation.row = (((rand() % HEIGHT) / 2) * 2);
    currentLocation.column = (((rand() % WIDTH) / 2) * 2);

    startLocation = currentLocation;
    grid[currentLocation.row][currentLocation.column] = START;
    player = currentLocation;
    do 
    {
        drawMaze();
        bool canMoveUp = !(currentLocation.row == 0 || grid[currentLocation.row - 2][currentLocation.column] != WALL);
        bool canMoveDown = !(currentLocation.row == HEIGHT - 1 || grid[currentLocation.row + 2][currentLocation.column] != WALL);
        bool canMoveLeft = !(currentLocation.column == 0 || grid[currentLocation.row][currentLocation.column - 2] != WALL);
        bool canMoveRight = !(currentLocation.column == WIDTH - 1 || grid[currentLocation.row][currentLocation.column + 2] != WALL);

        if (canMoveUp || canMoveDown || canMoveLeft || canMoveRight)
        {
            stack.push(currentLocation);

            //choose random location to dig
            bool moveFound = false;
            while (!moveFound)
            {
                int direction = rand() % 4;
                if (direction == 0 && canMoveUp)
                {
                    moveFound = true;
                    grid[currentLocation.row - 2][currentLocation.column] = PATH;
                    grid[currentLocation.row - 1][currentLocation.column] = PATH;
                    currentLocation.row -= 2;
                }
                else if (direction == 1 && canMoveDown)
                {
                    moveFound = true;
                    grid[currentLocation.row + 2][currentLocation.column] = PATH;
                    grid[currentLocation.row + 1][currentLocation.column] = PATH;
                    currentLocation.row += 2;
                }
                else if (direction == 2 && canMoveLeft)
                {
                    moveFound = true;
                    grid[currentLocation.row][currentLocation.column - 2] = PATH;
                    grid[currentLocation.row][currentLocation.column - 1] = PATH;
                    currentLocation.column -= 2;
                }
                else if (direction == 3 && canMoveRight)
                {
                    moveFound = true;
                    grid[currentLocation.row][currentLocation.column + 2] = PATH;
                    grid[currentLocation.row][currentLocation.column - 2] = PATH;
                    currentLocation.column += 2;
                }
            }
        }
        else if (!stack.empty())
        {
            currentLocation = stack.top();
            stack.pop();
        }
    } while (!stack.empty());
    addDestinationToGrid();
}

void Maze::addDestinationToGrid()
{

}

错误:

maze.obj : error LNK2005: "char (* grid)[1003]" (?grid@@3PAY0DOL@DA) already defined in Main.obj
maze.obj : error LNK2005: "class Coordinate player" (?player@@3VCoordinate@@A) already defined in Main.obj
C:\Users\Matthew\Desktop\Comp 345\MazeOOP\Debug\MazeOOP.exe : fatal error LNK1169: one or more multiply defined symbols found

2 个答案:

答案 0 :(得分:3)

在标题中声明全局变量并在源中定义它们,如

标题

extern char grid[WIDTH][HEIGHT];

来源

char grid[WIDTH][HEIGHT];

答案 1 :(得分:1)

正如链接器错误所说,你的问题是多重定义之一。

如果多个编译单元包含头文件,则行

const int HEIGHT = 1003;
const int WIDTH = 1003;
char grid[WIDTH][HEIGHT];

将在每个中创建grid的定义。

您需要做的是声明grid,但不要在头文件中定义它。

const int HEIGHT = 1003;
const int WIDTH = 1003;
extern char grid[WIDTH][HEIGHT];

告诉编译器,每次看到这些声明时,都会在其他地方定义grid

然后在一个编译单元(也就是源文件)中,在包含标题之后,添加一个定义

char grid[WIDTH][HEIGHT];

缺少extern告诉编译器这是一个定义。项目中必须有一个且唯一的编译单元才能执行此操作。如果没有,链接器将抱怨grid被引用但未定义。如果项目中有多个,则会再次出现多重定义错误。