2013-05-11 57 views
-6

這裏聲明是代碼:CIN不相剋這個範圍錯誤++

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

你的編譯器說第23行存在問題。什麼是第23行,你爲什麼期望它工作正確? – djechlin 2013-05-11 12:21:34

回答

9

在這裏:

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

你錯過了一個冒號。它應該是:

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

沒有第二個冒號,而不是使用範圍解析操作的,你聲明標籤稱爲std,其次是不合格的名稱cin(這就是爲什麼編譯器抱怨cin不在此範圍內聲明)。