2013-03-07 45 views
0

我正在編寫一個程序來搜索一系列素數,大約一半來檢查我的進度我決定建立它以確保一切正常工作好,我不斷收到錯誤LNK2019!它說這是一個無法解決的外部因素。我做了一些研究,但我對其中的任何東西都不甚瞭解。這是代碼。錯誤LNK2019 C++非常簡單的程序

#include <iostream> 

using namespace std; 

int singlePrime(int subjectNumber); 

int main() { 
    cout<<"Would you like to find a single prime number(1), or a range(2)?"<<endl; 

    int methodchoice; 
    cin>>methodchoice; 

    if(methodchoice ==1) { 
     int subjectNumber; 
     cout<<"Which number would you like to test for primeness?"<<endl; 
     cin>>subjectNumber; 
     int singlePrime(subjectNumber); 
    } 

    if(methodchoice==2) { 
     int lowRange; 
     int highRange; 

     cout<<"Input the low value for your range."<<endl; 
     cin>> lowRange; 

     cout<<"Input the high value for your range"<<endl; 
     cin>> highRange; 

     for (int index=lowRange; index<highRange;index++) { 
      if (index=highRange) { 
       break; 
      } 

      singlePrime(index); 
     } 
    } 
} 
+0

從它'singlePrime'的外觀。代碼在哪裏? – Thomas 2013-03-07 22:27:34

+0

首先不要使用'使用命名空間std'在這裏閱讀更多關於爲什麼它的壞[鏈接]的詳細信息(http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a -bad - 實踐 - 在-C)。然後,我覺得我必須提倡我爲初學者程序員提供一個新的問答網站的建議。在哪裏可以問初學者的問題和專家來到那裏,因爲然後想教而不是僅僅給出RTFM的答案。 http://area51.stackexchange.com/proposals/52242/beginner-programmers?referrer=YHFcRobXPDGfDpFmz1HCvA2 – AxelOmega 2013-03-07 22:29:15

+0

建議:您開始使用大寫字母的方法名稱;將幫助你區分變量名稱。 – 2013-03-07 22:29:17

回答

1

它可能檢舉此函數原型:

int singlePrime(int subjectNumber);

您還沒有爲該函數定義的主體。你需要實現它(或者至少給它一個虛擬的實現)。

3

在這裏聲明,你從來沒有定義功能:

int singlePrime(int subjectNumber); 

鏈接器抱怨,因爲您調用此功能,但它的身體是行不通發現。

要驗證這是問題,包含若干虛擬實現的定義替換聲明:

int singlePrime(int subjectNumber) 
{ 
    return 0; 
} 

另請注意,你有一個整數的無用的初始化這裏稱爲singlePrime

if (methodchoice ==1) { 
    int subjectNumber; 
    cout<<"Which number would you like to test for primeness?"<<endl; 
    cin>>subjectNumber; 
    int singlePrime(subjectNumber); 
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Why this? 
} 

你可能是指這條線做別的事情(最有可能調用singlePrime()函數),因爲singlePrime在該塊的範圍之外是不可見的。

+2

這不是你在這裏強調的函數聲明,它是一個'int' :) – jrok 2013-03-07 22:35:45

+0

@jrok:該死,你說得對:-)我應該休息一下。將編輯,謝謝 – 2013-03-07 22:36:28

+0

好變量聲明安迪,jrok。 – 2013-03-07 22:38:55

0

那麼,我的心理調試技巧指出了這個問題。下面的代碼:

int singlePrime(int subjectNumber); 

告訴存在一個名爲singlePrime函數,它接受一個int並返回一個int編譯器。

當然,你從來沒有提供該函數的代碼......編譯器假定它在一些其他的.cpp文件中,並說:「哦,好的,鏈接器會照顧到這一點。」

當連接器出現時,它會看到它應該找到一個名爲singlePrime的函數,它接受int並返回int。但是這個功能無處可尋。

簡單的修復,改變:

int singlePrime(int subjectNumber); 

int singlePrime(int subjectNumber) 
{ 
    // some code here to do whatever singlePrime is supposed to do 
    // be sure to return the correct number. For now, return the 
    // number of the beast! 

    return 666; 
} 

在你的代碼裏,你似乎試圖調用這個函數:

if (methodchoice ==1) { 
    int subjectNumber; 
    cout<<"Which number would you like to test for primeness?"<<endl; 
    cin>>subjectNumber; 
    int singlePrime(subjectNumber); // What? 
} 

但是這ISN」如何在C或C++中調用函數。你應該仔細看看你的書或課堂筆記。你會做這樣的事情:

// call singlePrime and store the result in a variable called 
// ret so that we can use it. 
int ret = singlePrime(subjectNumber); 

併爲未來的參考,如果你發佈你的完整錯誤信息,這將有助於。你知道,如果我們的水晶球由於太陽耀斑發生故障。