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

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 namespace 中定义。因此,您可以这样写:

std::cout

如果您想将其缩短为cout,则可以编写
using namespace std;

要么
using std::cout;

在写cout之前。

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

关于c++ - 错误: 'cout' : undeclared identifier; though I've included iostream header file in program,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21276217/

10-11 22:08
查看更多