Visual studio 2015 gives me errors upon creating a simple test console program

前端 未结 10 833
轮回少年
轮回少年 2020-12-05 07:57

Here is the code I am using.

#include \"stdafx.h\"
#include 

int main() {
    std::cout << \"hi\";

    return 0;
}

10条回答
  •  暖寄归人
    2020-12-05 08:16

    #include "stdafx.h"

    There is a well-known difference between the <...> and "..." includes: briefly, that the former is for library includes and the latter is for local includes.

    You mention that you were looking around for stdafx.h but couldn't find it in the compiler installation. This suggests that:

    1. You think stdafx.h is a library file (it is not, unless it's some MS-specific extension, which I doubt, although it is traditionally used as a default filename for precompiled headers by the same--if you have made one, which you almost certainly haven't).

    2. Because of 1., you haven't made a local file stdafx.h, and therefore this include directive should fail. If it hasn't, then something fishy is happening.


    As to your actual problem, I have some notes:

    1. is the C header, not the C++ one. If you're including from a C++ file (extension .cpp, probably, for MSVC), then you should use the C++ header . However, this shouldn't actually cause the problem.

    2. You aren't using the stdio anyway (at least not directly). You're using iostream, which you're properly including. If that include is the one that's causing the error, then iostream is trying to include it, can't, and your compiler installation is borked.

    3. Try the similar program:


    #include 
    int main() {
        std::cout << "hi" << std::endl;
        return 0;
    }
    

    I have just checked myself that this compiles and executes properly under Visual Studio 2015 Professional.

    If this program does not compile, I suggest reinstalling Visual Studio. In my experience, this often fixes these tricky setup issues.

提交回复
热议问题