简体   繁体   中英

Why do I get error “'cout' in namespace 'std' does not name a type” when I use “using cout = std::cout;”?

I'm trying to use shorter syntax and avoid using std:: everywhere, so I started using new alias syntax. In some examples I saw people using it this way:

using json = nlohmann::json;

and tried this with std:: , but with code below:

#include <iostream>

using cout = std::cout;

int main()
{
    cout << "Sometext";
    return 0;
}

but I get error 'cout' in namespace 'std' does not name a type . I know I can use

using std::cout;

but why using cout = std::cout; doesn't work?

EDIT:

To all that votes to close this question: I posted it, because I wasn't able to find a solution, by writing by error message. Yes, question mentioned as one that has solution for my problem describes what happens, but when someone gets this sort of error, he will not find a solution easily. I just didn't realize, that cout is an object. I've read some questions like that, but still had no clue what happens.

using cout = std::cout; refers to 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 .

As the error message said, it's just trying to tell you that std::cout doesn't refer to a type name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM