我编译了以下代码,但在 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;
}
错误内容为:
“wordcount.cpp(57):致命错误 C1010:查找预编译头时文件意外结束。您是否忘记将 '#include "stdafx.h"' 添加到源中?"
我不认为我需要头文件来运行此代码。
看看https://stackoverflow.com/a/4726838/2963099
关闭预编译头:
Project Properties -> C++ -> Precompiled Headers
将
Precompiled Header
设置为 "Not Using Precompiled Header"
。
创建一个新的“空项目”,将您的Cpp文件添加到新项目中,删除包含stdafx的行。
完成。
该项目不再需要stdafx。当您使用已安装的模板创建项目时,它会自动添加。