【问题标题】:'cin' does not name a type [closed]'cin' 没有命名类型[关闭]
【发布时间】:2021-08-17 09:35:32
【问题描述】:
#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();

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

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

我无法调试代码问题。有什么想法吗?

编辑:Atom

操作系统:Windows 10

编译器:MinGW

谢谢

【问题讨论】:

  • 你不能使用这个语句 cin >> N >> M;在函数之外,例如在 main 之外。
  • 多么奇怪,全局命令现在被降级了。
  • 我建议您从good book 开始,而不是尝试通过反复试验来学习像 c++ 这样的复杂语言。不过,这里不是辅导你的地方。
  • cin 是一个对象(它的类型是std::istream)。只能在功能块内读取cin。您正试图在功能块之外读取。在功能块之外,唯一允许的就是声明(类型、变量和函数)。 cin &gt;&gt; N &gt;&gt; M是一个语句,这样的语句只在功能块内部有效。

标签: c++ std iostream cin


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-21
    • 2022-01-12
    • 2017-12-08
    • 2022-01-21
    • 2023-04-07
    • 2021-02-03
    相关资源
    最近更新 更多