C++ cout gives undeclared identifier

前端 未结 5 625
闹比i
闹比i 2021-01-18 11:33

So, I have this question. Why does cout throws

error C2065: \'cout\' : undeclared identifier

I am using Visual Studio 2012 as an IDE and I

5条回答
  •  不思量自难忘°
    2021-01-18 12:07

     #include "iostream"
    

    should be

     #include 
    

    Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include

    By courtesy of @Jerry Coffin's answer:

    When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.

    When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).

    EDIT:

    However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.

提交回复
热议问题