我意識到有幾個像這樣的重複項,但是迄今爲止他們都沒有爲我工作。 我想在Ubuntu上使用g ++編譯C++非常簡單的程序,但它給我的範圍錯誤。'cout'不是'std'的成員並且'cout'未在此範圍內聲明
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
這給了我這個錯誤:
sudo g++ -v test.c
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ was not declared in this scope
我也試圖定義範圍,因爲許多其他職位說,但也沒有工作,但給了我一個不同的錯誤:
#include <iostream>
int main()
{
std::cout << "Hello world";
}
給出錯誤:
test.c: In function ‘int main()’:
test.c:6:3: error: ‘cout’ is not a member of ‘std’
在線大多數建議都建議使用「命名空間標準」,「#包括」和「std :: cout」。 所以我嘗試了所有3一起,仍然沒有運氣:(
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello world";
}
給出了錯誤:
test.c: In function ‘int main()’:
test.c:7:3: error: ‘cout’ is not a member of ‘std’
我已經經歷了幾個網上論壇,但他們都不爲工作我:(
這是一個更大的問題,因爲其中一個我的Linux製作不起作用的一部分
順便說一句,我使用g ++而不是gcc作爲幾個帖子搞砸了。
編輯1:
我改的.cpp,並沒有sudo執行的名字。以下是詳細輸出:
[email protected]:~/Desktop/SIP/SIPp/sipp-3.3$ g++ -v test.cpp -o test
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.7.2-2ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs --enable-languages=c,c++,go,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.7 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-gnu-unique-object --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.2 (Ubuntu/Linaro 4.7.2-2ubuntu1)
COLLECT_GCC_OPTIONS='-v' '-o' 'test' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
/usr/lib/gcc/x86_64-linux-gnu/4.7/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -version -fstack-protector -o /tmp/cczzibvL.s
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/4.7/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/include/c++/4.7
/usr/include/c++/4.7/x86_64-linux-gnu
/usr/include/c++/4.7/backward
/usr/lib/gcc/x86_64-linux-gnu/4.7/include
/usr/local/include
/usr/lib/gcc/x86_64-linux-gnu/4.7/include-fixed
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
GNU C++ (Ubuntu/Linaro 4.7.2-2ubuntu1) version 4.7.2 (x86_64-linux-gnu)
compiled by GNU C version 4.7.2, GMP version 5.0.2, MPFR version 3.1.0-p3, MPC version 0.9
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 521527ea42f0901bf839bcaad0cb13e6
test.cpp: In function ‘int main()’:
test.cpp:5:3: error: ‘cout’ is not a member of ‘std’
你爲什麼需要'sudo'來運行g ++?文件擴展名應該是'.cpp'而不是'.c'? – billz 2013-03-12 11:12:25
重命名您的源文件以'.cpp'結尾爲'g ++'提供正確的線索。 – 2013-03-12 11:12:56
不能重現... – NPE 2013-03-12 11:13:49