首页 > 解决方案 > 'cin' 没有命名类型

问题描述

#include <iostream>
#include <string.h>
using namespace std;

/* Variables */
int N = 0;
int M = 0;
int Px, Py;
int gcount = 0;
cin >> N >> M;
string maze[100];

/* Define Functions */
void read_maze(int);
void scan_maze(int);
void print_maze();

这是我正在制作的基于文本的游戏的代码片段,当我运行它时,我收到以下错误:

main.cpp:10:1: error: 'cin' does not name a type
   10 | cin >> N >> M;
      | ^~~

我无法用代码调试问题。有任何想法吗?

编辑:原子

操作系统:Windows 10

编译器:MinGW

谢谢

标签: c++stdiostreamcin

解决方案


您必须在函数内使用语句,例如

#include <iostream>

int main() {
    /* Variables are fine at global scope, but you should prefer local ones */
    int N = 0;
    int M = 0;
    int Px, Py;
    int gcount = 0;
    
    /* Here's the problematic statement from before */
    std::cin >> N >> M;
    ...
}

推荐阅读