【问题标题】:Error 'map' was not declared in this scope [closed]在此范围内未声明错误“地图”[关闭]
【发布时间】:2014-04-27 04:30:21
【问题描述】:

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

错误具体说明:

project.cpp:52:7: 错误:'map' 未在此范围内声明

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

【问题讨论】:

  • 开头的“if(map[y2][x] == '^') {”是什么?
  • 不要使用using namespace std。这真的很有害,因为在这种情况下它隐藏了您的 map 变量。

标签: c++ linux compiler-errors


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

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

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

【讨论】:

  • 好的,我将它重命名为 map 以外的 diff 变量名称,因为与 STD 冲突。此外,将“帮助勇敢的冒险家...”移至主目录。现在我的主要问题是,我能够在 Windows 上运行它,显然是对代码进行了一些更改,你能否向我推荐好的材料来让运动响应击键?我使用箭头键在 windows 中移动,但由于找不到 AsyncKey 的绝佳替代品,我试图将移动分配给 W、A、S、D,但编译和运行后,它没有响应。跨度>
  • @user3577531 我认为你最好为此提出一个新问题
【解决方案2】:

将第一行 if(map[y2][x] == '^') { 视为错字,

以下需要修复,至少可以编译运行**

首先,cout &lt;&lt; "Help the Brave adventurer...应该放在main里面

其次,修复以下问题:

char map[17][21] = 
{               
       "Use W,A,S,D to move", // add comma
       "Press X to end game", // add comma
       // ....
} ;

**好像也有逻辑错误!请不要在这里全部问。

【讨论】:

  • 这就是我现在正在做的事情。我一直无法找到好的参考资料来帮助我理解如何为按键分配移动,因此它会更新地图。我可以让它在 Windows 中使用 Async,但很难在 Linux 中找到类似的解决方案。
  • @user3577531 但这不是你的问题,对吧?从来不建议在评论中提出一个全新的问题,也不要假设人们会为您解决所有语法和逻辑错误,谢谢
  • 好的,很抱歉。我只是想了解如何让它工作,从 Windows 过渡到 Linux
  • @user3577531 “我只是想了解...”应该是“我现在想了解...”。您当前的程序不会在任何平台上编译,不确定您是如何在 Windows 上编译的。您现在应该提出一个新问题,如果您的错误得到解决,谢谢
  • 这不是windows程序的样子。这是经过多次转换/更改以尝试使其在 linux 上工作。我不是专业人士,也不假装是。
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-17
  • 1970-01-01
  • 2021-12-16
  • 2013-10-25
相关资源
最近更新 更多