当我使用 "using cout = std::cout;" 时,为什么会出现错误“'cout' in namespace 'std' does not name a type”?
Why do I get error "'cout' in namespace 'std' does not name a type" when I use "using cout = std::cout;"?
我试图使用更短的语法并避免在任何地方使用 std::
,所以我开始使用新的别名语法。在一些例子中,我看到人们这样使用它:
using json = nlohmann::json;
并尝试使用 std::
,但代码如下:
#include <iostream>
using cout = std::cout;
int main()
{
cout << "Sometext";
return 0;
}
但我收到错误 'cout' in namespace 'std' does not name a type
。我知道我可以使用
using std::cout;
但为什么 using cout = std::cout;
不起作用?
编辑:
对于所有投票结束这个问题的人:我发布了它,因为我无法通过错误消息编写来找到解决方案。是的,作为对我的问题有解决方案而提到的问题描述了发生的情况,但是当有人遇到这种错误时,他不会轻易找到解决方案。我只是没有意识到 cout
是一个对象。我读过一些类似的问题,但仍然不知道会发生什么。
using cout = std::cout;
指的是type alias declaration syntax. It's similar to typedef
; so you're trying to declare a type named cout
that refers to a previously defined type std::cout
. But std::cout
is not a type name, it's an object with type of std::ostream
。
如错误消息所述,它只是想告诉您 std::cout
没有引用类型名称。
我试图使用更短的语法并避免在任何地方使用 std::
,所以我开始使用新的别名语法。在一些例子中,我看到人们这样使用它:
using json = nlohmann::json;
并尝试使用 std::
,但代码如下:
#include <iostream>
using cout = std::cout;
int main()
{
cout << "Sometext";
return 0;
}
但我收到错误 'cout' in namespace 'std' does not name a type
。我知道我可以使用
using std::cout;
但为什么 using cout = std::cout;
不起作用?
编辑:
对于所有投票结束这个问题的人:我发布了它,因为我无法通过错误消息编写来找到解决方案。是的,作为对我的问题有解决方案而提到的问题描述了发生的情况,但是当有人遇到这种错误时,他不会轻易找到解决方案。我只是没有意识到 cout
是一个对象。我读过一些类似的问题,但仍然不知道会发生什么。
using cout = std::cout;
指的是type alias declaration syntax. It's similar to typedef
; so you're trying to declare a type named cout
that refers to a previously defined type std::cout
. But std::cout
is not a type name, it's an object with type of std::ostream
。
如错误消息所述,它只是想告诉您 std::cout
没有引用类型名称。