2013-11-21 11 views
30

我編譯下面的代碼,但我在Visual Studio中遇到了編譯錯誤,我無法理解。致命錯誤C1010 - VisualStudio中的「stdafx.h」如何糾正?

#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:在查找預編譯頭文件的意外結束你忘了加上「#包括‘stdafx.h中’ '來源?'

我不相信我需要一個頭文件來運行此代碼。感謝您提前給予您所有的幫助。

+0

如果錯誤消息提示有變化,爲什麼不試試看看會發生什麼? – abiessu

+0

我做到了。只有更多的錯誤出現。不止一個錯誤。 – user1800967

+0

哪個編譯器?什麼OS?什麼是出現的新錯誤的一些例子?什麼是你的編譯設置和/或什麼是你的編譯命令? – abiessu

回答

64

https://stackoverflow.com/a/4726838/2963099

關閉預編譯頭:

Project Properties -> C++ -> Precompiled Headers 

設置Precompiled Header"Not Using Precompiled Header"

+1

@ user1800967 - 在VS2010中 - 'Common Properties'->'Configuration Properties'->'C/C++' - >'預編譯頭文件';那麼'預編譯頭文件'應該是'不使用預編譯頭文件' –

+0

好吧,我明白了。正在運行... – user1800967

+0

@ user1800967沒有更多的錯誤? –

15

項目所有的源文件的第一行必須是以下幾點:

#include <stdafx.h> 

訪問here瞭解預編譯頭

+2

不正確,答案是關閉預編譯頭文件。 – john

+1

@john無論哪種方式工作。預編譯頭文件可以(但不總是)顯着提高編譯速度,儘管它們也有一些缺點。如果user1800967想要使用預編譯頭文件,他可以添加#include 作爲源文件中的第一個非註釋行。 asif對於需要成爲源文件中的第一行是不正確的,但它確實需要是源文件中的第一個非註釋行。 – George

4

創建一個新的「空項目」,添加您的將Cpp文件添加到新項目中,刪除包含stdafx的行。

完成。

該項目不再需要stdafx。當您使用安裝的模板創建項目時,它會自動添加。 enter image description here

+0

這適用於使用Visual Studio 2017社區版。在64位模式下的第一次嘗試進行編譯,並提供一些關於可能導致數據丟失的數字轉換的警告。 –