Home c++ Unexpected end of file while searching for precompiled header

Unexpected end of file while searching for precompiled header

Author

Date

Category

Hello, there is the following code:

# include & lt; iostream & gt;
using namespace std;
int Func ();
int main () {
  Func ();
  return 0;
}
int Func () {
  cout & lt; & lt; "Hello world" & lt; & lt; endl;
  cout & lt; & lt; "2 + 2 =" & lt; & lt; 2 + 2 & lt; & lt; endl;
  return 0;
}

However, when executed, it throws an error:

c: \ users \ ok \ source \ repos \ consoleapplication4 \ consoleapplication4 \ consoleapplication4.cpp (15): fatal error C1010: Unexpected end of file while searching for precompiled header. You may have forgotten to add the “#include” stdafx.h “” directive to the source.

I add #include “stdafx.h”, it still gives an error:

1 & gt; c: \ users \ ok \ source \ repos \ consoleapplication4 \ consoleapplication4 \ consoleapplication4.cpp (12): error C2065: cout: undeclared identifier
1 & gt; c: \ users \ ok \ source \ repos \ consoleapplication4 \ consoleapplication4 \ consoleapplication4.cpp (12): error C2065: endl: undeclared identifier
1 & gt; c: \ users \ ok \ source \ repos \ consoleapplication4 \ consoleapplication4 \ consoleapplication4.cpp (13): error C2065: cout: undeclared identifier
1 & gt; c: \ users \ ok \ source \ repos \ consoleapplication4 \ consoleapplication4 \ consoleapplication4.cpp (13): error C2065: endl: undeclared identifier


Answer 1, authority 100%

When creating a new project, you selected “Console Application”. It contains “stdafx.h” by default. I suggest choosing “Empty Project” and writing in it from scratch. If you want to leave this project and fix it, then put #include “stdafx.h” at the very top, before the rest of the include directives. You can write all the includes into it, and then it will compile faster.


Answer 2

Alternatively, you can turn off the “precompiled header”.
Go to Project – & gt; Properties – & gt;

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions