c++ - 错误 'map' 未在此范围内声明

标签 c++ linux compiler-errors

试图完成我的个人项目,但我陷入了这个问题。我正在制作一款 2d 游戏,并认为我已经设置得很好,但我得到了一个错误,这基本上表明我的变量之一没有在多个范围中声明。这是我的代码,试图制作一个简单的 2d 游戏,试图在 Linux 中编译和运行。

错误具体指出:

project.cpp:52:7: error: 'map' was not declared in this scope

 if(map[y2][x] == '^') {


#include <iostream>
#include <cstdlib>

using namespace std;

cout << "Help the Brave adventurer, &, find his way through the maze, to reach the Treasure, $$!"

char map[17][21] = {                
    "Use W,A,S,D to move"
    "Press X to end game"   
    "####################",
    "# & ##      ##  # ^#",
    "#   ##^        ^# ^#",
    "#   ##      ## ^#  #",
    "#  ^##     ^##     #",
    "#           ##  #^ #",
    "#^          #####  #",
    "#   ##         ^#  #",
    "#   ##^^    #^  #  #",
    "# ^^##  #####   #  #",
    "#   ##  #     ^    #",
    "#   ##  #^ ^#   #  #",
    "#   ##^        ^#$$#",
    "####################",

};

int x = 1;      
int y = 1;      
int hp = 5;

bool game_run = true;

int main ()
{
    while(game_run == true) {   
        system("clear");        
        for(int showMap=0; showMap<17; showMap++) {
            cout << map[showMap]
        }
        system("pause > nul");   

        if(cin.get() == 's'){           
            int y2 = y+1;
            if(map[y2][x] == ' ') { 
                map[y][x] = ' ';
                y++; 
                map[y][x] = '&';    
            }
            if(map[y2][x] == '^') {
                hp--;
            }
        }
        if(cin.get() == 'w'){           
            int y2 = y-1;
            if(map[y2][x] == ' ') {     
                map[y][x] = ' ';
                y--;                                        
                map[y][x] = '&';
            }
            if(map[y2][x] == '^') {
                hp--;
            }   
        }
        if(cin.get() == 'd'){
            int x2 = x+1;
            if(map[y][x2] == ' ') {
                map[y][x] = ' ';
                x++;
                map[y][x] = '&';
            }
            if(map[y][x2] == '^') {
                hp--;
            }
        }
        if(cin.get() == 'a'){
            int x2 = x-1;
            if(map[y][x2] == ' ') {
                map[y][x] = ' ';
                x--;
                map[y][x] = '&';
            }
            if(map[y][x2] == '^') {
                hp--;
                cout << "You have run into spikes!!!  Health has been decreased by 1...";
            }
        }
        if(cin.get() == 'x'){
            game_run = false;
        }
        if(hp == 0) {
            game_run = false;
            cout << "The hero has died.... game over... :(";
        }
    }   

    system("clear");
    cout << "Game Over";

    return 0;
}

最佳答案

  1. 尝试将变量重命名为“map ”以外的名称,因为它也代表 std::map .

  2. 第一行“if(map[y2][x] == '^') {”是拼写错误吗?

  3. 我认为你必须移动它 cout << "Help the Brave adventurer, &, find his way through the maze, to reach the Treasure, $$!"进入main()

关于c++ - 错误 'map' 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23319411/

相关文章:

c++ - boost::uuid/unique 跨不同数据库

c++ - 无法编译,错误 : cryptlib. h:没有那个文件或目录

linux - ps aux如何统计特定用户的java进程

cmake - 找不到基本目录,请将BASE_DIRECTORY设置为CMake中包含shared_sources的文件夹

java - 为什么我不能以这种方式在 for 循环中定义变量?

c++ - utf8 字符计数不起作用

c - 如何为 Arm Linux 构建静态版本的 pcap 库

linux - 类型错误 : __init__() got an unexpected keyword argument 'timeout' pymongo

Java:找不到符号(构造函数)

c++ - 函数指针调用不编译