C++ “cout”不是“std”的成员,并且“cout”未在此范围内声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15359589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
‘cout’ is not a member of ‘std’ & ‘cout’ was not declared in this scope
提问by Pranoy C
I realize that there are several duplicates like this but none of them have worked for me so far. I am trying to compile a C++ very simple program on Ubuntu using g++ but it is giving me scope errors.
我意识到有几个这样的重复项,但到目前为止没有一个对我有用。我正在尝试使用 g++ 在 Ubuntu 上编译一个 C++ 非常简单的程序,但它给了我范围错误。
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world";
}
This gives me this error:
这给了我这个错误:
sudo g++ -v test.c
test.c: In function ‘int main()':
test.c:7:3: error: ‘cout' was not declared in this scope
I also tried defining the scope as many other posts say, but that also didn't work, but gave me a different error:
我也尝试像许多其他帖子所说的那样定义范围,但这也不起作用,但给了我一个不同的错误:
#include <iostream>
int main()
{
std::cout << "Hello world";
}
Gives error:
给出错误:
test.c: In function ‘int main()':
test.c:6:3: error: ‘cout' is not a member of ‘std'
Most of the suggestions online suggest "using namespace std;", "#include " and "std::cout". So I tried all 3 together, still no luck :(
大多数在线建议都建议“使用命名空间 std;”、“#include”和“std::cout”。所以我同时尝试了所有 3 个,但仍然没有运气:(
#include <iostream>
using namespace std;
int main()
{
std::cout << "Hello world";
}
gives error:
给出错误:
test.c: In function ‘int main()':
test.c:7:3: error: ‘cout' is not a member of ‘std'
I have gone through several forums online but none of them seem to work for me :(
我在网上浏览了几个论坛,但似乎没有一个对我有用:(
This is a part of a bigger issue because of which one of my linux make doesn't work.
这是一个更大问题的一部分,因为我的哪个 linux make 不起作用。
Btw, I am using g++ and not gcc as a few posts messed up.
顺便说一句,我使用的是 g++ 而不是 gcc,因为一些帖子搞砸了。
EDIT 1:
编辑 1:
I changed the name to .cpp, and execute without sudo. Here is the verbose output:
我将名称更改为 .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'
回答by teppic
What you've written works absolutely fine on my Ubuntu system, with the same version of g++.
你写的东西在我的 Ubuntu 系统上运行得非常好,使用相同版本的 g++。
It sounds like you haven't installed all of the necessary files for the C++ environment, or something isn't quite right with it. Try this:
听起来您还没有为 C++ 环境安装所有必需的文件,或者它的某些地方不太正确。尝试这个:
$ sudo apt-get remove g++ libstdc++-6.4.7-dev
$ sudo apt-get install build-essential g++-multilib
(Run dpkg -l | grep libstdc++
to get the exact version of libstdc++ if the above fails)
(dpkg -l | grep libstdc++
如果上述操作失败,请运行以获取 libstdc++ 的确切版本)
回答by superbriggs
This isn't the case for this, but could be an answer to "error: ‘cout' is not a member of ‘std'". I came here looking for help, so this might help someone else.
情况并非如此,但可能是“错误:'cout' 不是'std' 的成员”的答案。我来这里寻求帮助,所以这可能会帮助其他人。
If you have a header file (header.h) where the last function/class declaration does not have a semi-colon after it:
如果您有一个头文件 (header.h),其中最后一个函数/类声明后面没有分号:
#include <string>
function(std::string str)
And your c++ file includes this before iostream
:
并且您的 C++ 文件之前包含此内容iostream
:
#include "header.h"
#include <iostream>
The function/class (function
in this case) messes up the declarations in iostream
. #include
basically just pastes the text into the code. Adding the semi-colon can fix the problem.
函数/类(function
在这种情况下)弄乱了iostream
. #include
基本上只是将文本粘贴到代码中。添加分号可以解决问题。
Hope that helped someone. I think @Farhad has caught the actual problem in this question though.
希望对某人有所帮助。不过,我认为@Farhad 已经解决了这个问题中的实际问题。
回答by Ekramul Hoque
Rename file extension with .cpp . Complie source code by command gcc source.cpp -o output.out . GCC will automatically compile it as a C++ program.
使用 .cpp 重命名文件扩展名。通过命令 gcc source.cpp -o output.out 编译源代码。GCC 会自动将其编译为 C++ 程序。
回答by Ankit Jain
For C++ programs, use g++, not gcc. If you get errors that suggest your compiler cannot find the standard library, that's because you probably used gcc.
对于 C++ 程序,使用 g++,而不是 gcc。如果您收到提示您的编译器找不到标准库的错误,那是因为您可能使用了 gcc。
回答by Arthur Zennig
(C++, Linux Terminal, no compiling error, but no prints into the Terminal), maybe try:
(C++,Linux 终端,没有编译错误,但没有打印到终端中),也许可以尝试:
$ g++ yourcode.cpp
$ ./a.out
first line: g++ compiles your code.
second line: runs your compiled code.
第一行:g++ 编译你的代码。
第二行:运行你编译的代码。