2011-03-12 98 views
2

**編輯:除了錯誤指出下面,我被錯誤地試圖編譯爲一個Win32項目,按照該錯誤代碼「不是所有的控制路徑返回一個值」

1> MSVCRTD .lib(crtexew.obj):錯誤LNK2019:無法解析的外部符號WinMain @ 16>在函數中引用_ _tmainCRTStartup 因此避免危機並完成作業。非常感謝你的幫助。希望當我知道一件或兩件東西時,我可以同樣向社區付錢**

在這項任務中,我們應該使用遞歸作爲確定單詞是否符合迴文的技巧。儘管我仍然在努力使它成爲解決問題的機制,但這段代碼看起來應該是正常的。但是,編譯器給我「不是所有的控制路徑返回一個變量」的錯誤。有任何想法嗎?

#include<iostream> 
#include<fstream> 
#include<string> 
using namespace std; 

bool palcheck(string word, int first, int last); 

int main() 
{ 
    ofstream palindrome, NOTpalindrome; 
    ifstream fin; 
    string word; 

    palindrome.open("palindrontest.txt"); 
    NOTpalindrome.open("notPalindronetest.txt"); 
    fin.open("input5.txt"); //list of palindromes, one per line 

    if (palindrome.fail() || NOTpalindrome.fail() || fin.fail()) 
    return -1; 

    while (fin >> word) 
    { 
    if (palcheck(word, 0, (word.size()-1)) == true) 
     palindrome << word << endl; 
    else 
     NOTpalindrome << word << endl; 
    } 

    palindrome.close(); 
    NOTpalindrome.close(); 
    fin.close(); 

    return 0; 
} 

bool palcheck(string word, int first, int last) 
{ 
if (first >= last) 
return true; 

else if (word[first] == word[last]) 
return palcheck(word, first+1, last-1); 

else// (word[first] != word[last]) 
return false; 

} 
+0

中加入「決定」的稱號(我已經刪除它)取而代之,你應該接受一個答案,因爲它是在這裏做,所以:) – Stormenet 2011-03-21 22:03:14

回答

2

的錯誤是在你的palcheck功能,你忘了返回遞歸函數

bool palcheck(string word, int first, int last) 
{ 
if (first == last) 
return true; 

else if (word[first] == word[last]) 
return palcheck(word, first+1, last-1); // <- this return ;) 

else// ((word[first] != word[last]) || (first > last)) 
return false; 
} 
+0

感謝的方式你但是,我仍然留下了:1> MSVCRTD.lib(crtexew.obj):錯誤LNK2019:無法解析的外部符號_WinMain @ 16在函數___中引用了___tmainCRTStartup 1> C:\ Users \ dd \ Documents \ Visual Studio 2010 \ Projects \ CS201 \ program5 \ Debug \ program5.exe:致命錯誤LNK1120:1無法解析的外部「 – derek 2011-03-12 16:53:11

2

的錯誤是在你的palcheck函數的值。如果else if中的表達式爲真,則該函數不會返回值。

下應該修復它:

bool palcheck(string word, int first, int last) 
{ 
    if (first == last) 
    return true; 

    else if (word[first] == word[last]) 
    return palcheck(word, first+1, last-1); 

    else// ((word[first] != word[last]) || (first > last)) 
    return false; 
} 
+0

感謝您的幫助,但我做了您所建議的更改, 「1> MSVCRTD.lib(crtexew.obj):錯誤LNK2019:無法解析的外部符號_WinMain @ 16引用在功能___tmainCRTStartup 1> C:\ Users \ dd \ Documents \ Visual Studio 2010 \ Projects \ CS201 \ program5 \ Debug \ program5.exe:致命錯誤LNK1120:1無法解析的外部「 – derek 2011-03-12 16:49:28

1
if (first == last) 

需求是

if (first >= last) 

的情況下有偶數個字母。

+0

謝謝,少一個問題,我將不得不排查一次,我終於得到程序運行! – derek 2011-03-12 16:51:01

1
bool palcheck(string word, int first, int last) 
{ 
if (first == last) 
    return true; 
else if (word[first] == word[last]) 
    palcheck(word, first+1, last-1); // <-here 
else 
    return false; 

// <-reaches here 
} 

上述問題標記爲here。當選擇這種情況時,你不會從函數返回任何東西,這是一個問題。你或許應該使用:

return palcheck(word, first+1, last-1); 
相關問題