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

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

本文关键字:程序 包含 文件 iostream 未声明 cout 标识符 错误      更新时间:2023-10-16

我试图在下面编译简单的程序。但是,它不是编译&给出错误:

error C2065: 'cout' : undeclared identifier

我想问你,尽管我在其中包含了 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;
    }

预先感谢。

COUT流在STD名称空间中定义。为了命名您写的:

std::cout

如果您想将其缩短到cout,则可以写

using namespace std;

using std::cout;

写Cout。

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

您必须编写std::cout或添加using std;