C++ 错误:'cout':未声明的标识符;虽然我已经在程序中包含了 iostream 头文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21276217/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:35:23  来源:igfitidea点击:

Error: 'cout' : undeclared identifier; though I've included iostream header file in program

c++namespacesiostreamcout

提问by yuvi

I am trying to compile the simple program below. But, it's not compiling & gives error:

我正在尝试编译下面的简单程序。但是,它没有编译并给出错误:

error C2065: 'cout' : undeclared identifier

I want to ask you that why this program doesn't work though I've included iostreamheader file in it?

我想问你,为什么这个程序虽然我已经包含了iostream头文件,但它不起作用?

#include <iostream>

void function(int) { cout << “function(int) called” << endl; }
void function(unsigned int) { cout << “function(unsigned int) called” << endl; }
    int main()
    {
        function(-2);
        function(4);
        return 0;
    }

Thanks in advance.

提前致谢。

回答by David Heffernan

The cout stream is defined in the std namespace. So to name it you write:

cout 流在 std 命名空间中定义。所以命名它你写:

std::cout

If you want to shorten this to cout then you can write

如果你想缩短这个 cout 那么你可以写

using namespace std;

or

或者

using std::cout;

before writing cout.

在写 cout 之前。

Any good documentation source will tell you which namespace contains an object. For instance: http://en.cppreference.com/w/cpp/io/cout

任何好的文档源都会告诉您哪个命名空间包含一个对象。例如:http: //en.cppreference.com/w/cpp/io/cout

回答by Uri Y

You have to write std::coutor add using std;

你必须写std::cout或添加using std;