gpt4 book ai didi

c++ - 错误 : 'cout' : undeclared identifier; though I've included iostream header file in program

转载 作者:IT老高 更新时间:2023-10-28 22:26:33 27 4
gpt4 key购买 nike

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

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

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com