cin没有在这个范围内用g ++声明错误

时间:2013-05-11 12:16:49

标签: c++ g++

以下是代码:

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

这是错误:

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’

我已经纠正了代码,有几个错误(这是我的第一个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;
}

1 个答案:

答案 0 :(得分:11)

下面:

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

你错过了冒号。它应该是:

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

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