C++ cin 未在此范围内使用 g++ 错误声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16496978/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:24:43  来源:igfitidea点击:

cin not declared in this scope error with g++

c++g++

提问by qed

Here is the code:

这是代码:

#include <stdlib.h>
#include <iostream>
int main() {
    std::cout << "Enter two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std:cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << "and " v2 << "is " << v1+v2 << std:endl;
    return 0;
}

Here is the error:

这是错误:

g++ x.cpp 
#x.cpp: In function ‘int main()':
#x.cpp:23:9: error: ‘cin' was not declared in this scope
#x.cpp:23:9: note: suggested alternative:
#In file included from x.cpp:19:0:
#/usr/include/c++/4.7/iostream:61:18: note:   ‘std::cin'
#x.cpp:24:48: error: expected ‘;' before ‘v2'


I have corrected the code, there are several mistakes (this is my first c++ experience):

我已经更正了代码,有几个错误(这是我的第一次c++体验):

#include <stdlib.h>
#include <iostream>
int main() {
    std::cout << "Enter two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The sum of " << v1 << " and " <<  v2 << " is " << v1+v2 << std::endl;
    return 0;
}

回答by Andy Prowl

Here:

这里:

std:cin >> v1 >> v2;
// ^

You are missing a colon. It should be:

你少了一个冒号。它应该是:

std::cin >> v1 >> v2;
// ^^

Without the second colon, instead of using the scope resolution operator, you are declaring a labelcalled std, followed by an unqualified name cin(which is why the compiler complains about cinnot being declared in this scope).

没有第二个冒号,而不是使用范围解析运算符,您将声明一个名为的标签std,后跟一个非限定名称cin(这就是编译器抱怨cin未在此范围内声明的原因)。