2011-03-12 100 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); 
相关问题