gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:45:20 25 4
gpt4 key购买 nike

在我的程序中,我正在 build 一个迷宫。每当我构建我的项目时,我都会收到这些错误。我知道这些错误是指我的头文件中的第 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

最佳答案

在header中声明全局变量,在source中定义,如

标题

extern char grid[WIDTH][HEIGHT];

来源

char grid[WIDTH][HEIGHT];

关于c++ - 构建 C++ 程序时出现多个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34781496/

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