-2

这是代码:

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

1 回答 1

14

这里:

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

您缺少一个冒号。它应该是:

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

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

于 2013-05-11T12:17:16.780 回答