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

  • A+
Category:Languages

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: