致命错误C1010 - Visual Studio中的"stdafx.h"如何纠正?

use*_*967 46 c++ compiler-construction header visual-studio

我编译以下代码,但我在Visual Studio中得到一个我无法理解的编译错误.

#include <iostream>

using namespace std;

int main()
{
    int matchCount, findResult;
    long childPID;
    string userInput = "blank";

    // string to be searched through
    string longString = "The PPSh-41 is a Soviet submachine gun designed by Georgi Shpagin as an inexpensive, simplified alternative to the PPD-40.";

    while (userInput.compare("!wq"));
    {
        // reset variables for reuse
        matchCount = 0;
        findResult = -1;

        cout << "Please enter a word/s to search for (!wq to exit): "; // prompts user for string to search for
        cin >> userInput; // takes user input

        if (userInput.compare("!wq")) // checks user input to see if they still wish to search for a string
        {
            childPID = fork();

            if (childPID == 0)
            {
                while (findResult < longString.length)
                {
                    findResult = longString.find(userInput, findResult + 1, userInput.length);

                    if (findResult < longString.length)
                        matchCount++;
                }

                cout << "There are " << matchCount << " instances of " << userInput << " in longString." << endl;
            }
            else
                cout << "childPID != 0" << endl;
        }
        else
            cout << "User has chosen to exit. Exiting." << endl;
    }

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误如下:

"wordcount.cpp(57):致命错误C1010:在查找预编译头文件时意外结束文件.您是否忘记在源代码中添加"#include"stdafx.h"'?"

我不相信我需要一个头文件来运行此代码.提前感谢您的帮助.

Gle*_*aum 103

请访问/sf/answers/330878691/

关闭预编译的标题:

Project Properties -> C++ -> Precompiled Headers
Run Code Online (Sandbox Code Playgroud)

设置Precompiled Header"Not Using Precompiled Header".

  • @ user1800967 - 在VS2010中 - `Common Properties`->`Configuration Properties`->`C/C++` - >`Precompiled Headers`; 那么`预编译的标题`应该是'不使用预先排序的标题' (3认同)
  • 值得注意的是,如果您仍然遇到相同的错误,可能是因为您没有在正确的发布配置或平台中关闭该选项。 (3认同)

asi*_*sif 21

项目的每个源文件的第一行必须如下:

#include <stdafx.h>
Run Code Online (Sandbox Code Playgroud)

访问此处以了解预编译标题

  • 看起来不对劲 它是`#include"stdafx.h"`,而不是`#include <stdafx.h>`. (6认同)
  • 不是这样,答案是关闭预编译的头文件. (5认同)
  • 这应该是公认的答案。说“如果它不适合您,请关闭该功能”的解决方案根本不是真正的解决方案。 (5认同)
  • @john无论哪种方式都有效.预编译头可以(但不总是)显着提高编译速度,尽管它们也有一些缺点.如果user1800967想要使用预编译头文件,他可以将#include <stdafx.h>添加为源文件中的第一个非注释行.asif是不正确的,它必须是源文件中的第一行,但它确实需要是源文件中的第一个非注释行. (2认同)

Zah*_*ouf 5

创建一个新的" 空项目 ",将您的Cpp文件添加到新项目,删除包含stdafx的行.

完成.

该项目不再需要stdafx.使用已安装的模板创建项目时会自动添加它. 在此输入图像描述