Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 226
Staszek Avatar asked Dec 02 '22 11:12

Staszek


1 Answers

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.

like image 64
songyuanyao Avatar answered Dec 20 '22 05:12

songyuanyao