'cout' 不是 'std' 的成员 & 'cout' 未在此范围内声明

新手上路,请多包涵

我意识到有几个这样的重复,但到目前为止它们都没有为我工作。我正在尝试使用 g++ 在 Ubuntu 上编译一个非常简单的 C++ 程序,但它给了我范围错误。

 #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;”、“#include”和“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 make 不起作用。

顺便说一句,我使用的是 g++ 而不是 gcc,因为一些帖子搞砸了。


编辑1:

我将名称更改为 .cpp,并在没有 sudo 的情况下执行。这是详细的输出:

 pranoy@pranoyubuntu1210:~/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’

原文由 sudoExclaimationExclaimation 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 2k
2 个回答

您所写的内容在我的 Ubuntu 系统上运行良好,使用相同版本的 g++。

听起来您还没有为 C++ 环境安装所有必要的文件,或者有些地方不太对劲。尝试这个:

 $ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib

(如果上述操作失败,请运行 dpkg -l | grep libstdc++ 以获取 libstdc++ 的确切版本)

原文由 teppic 发布,翻译遵循 CC BY-SA 3.0 许可协议

(C++,Linux 终端,没有编译错误, 但没有打印到终端),也许试试:

 $ g++ yourcode.cpp
$ ./a.out

第一行: g++ 编译你的代码。

第二行: 运行您编译的代码。

原文由 Arthur Zennig 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题