gpt4 book ai didi

c++ - 为什么 "identifier"被认为与 "identifier()"有歧义,当它们被不同地使用时?

转载 作者:行者123 更新时间:2023-11-30 02:16:03 26 4
gpt4 key购买 nike

这只是我偶然发现的例子!

我正在使用 coutoperator<<为什么这个程序不能编译?为什么它们不像函数重载那样被考虑?

#include <iostream> // imports the declaration of std::cout

using namespace std; // makes std::cout accessible as "cout"

int cout() // declares our own "cout" function
{
return 5;
}

int main()
{
cout << "Hello, world!"; // Compile error!

return 0;
}

最佳答案

在尝试插入流时,有两个名称 cout在全局范围内:一个来自标准库,std::cout , 被那个 hell 拉入全局范围 using声明,一个定义为函数 int cout() .在表达式中

cout << "Hello, world!\n";

使用cout是模棱两可的。没有函数重载有两个原因:第一,std::cout不是一个函数,所以不会参与重载。但更根本的是,使用 cout在该表达式中不是函数调用,因此,同样,没有重载。姓名cout来自函数定义的被视为指向函数的指针,名称 cout来自命名空间 std是对象的名称。该名称有两种可能的解释,因此它在 << 中的使用表达不明确。

关于c++ - 为什么 "identifier"被认为与 "identifier()"有歧义,当它们被不同地使用时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445145/

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