“并非所有控制路径都返回值”
**编辑:除了下面指出的错误之外,我还错误地尝试根据此错误代码将其编译为 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;
}
**EDIT: in addition to the mistakes pointed out below, i was mistakenly trying to compile it as a Win32 project, per this error code
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol WinMain@16 >referenced in function __tmainCRTStartup
so crisis averted and homework complete. Thank you all very much for your help. hopefully i can similarly pay the community back when i know a thing or two.**
In this assignment we're supposed to use recursion as a technique for determining whether a word qualifies as a palindrome. Although i'm still struggling to become comfortable with it as a problem solving mechanism, this code seems like it should otherwise work. However, the compiler gives me the "not all control paths return a variable" error. Any ideas?
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
错误出现在您的
palcheck
函数中。如果else if
中的表达式为 true,则该函数不会返回值。以下应该修复它:
The error is in your
palcheck
function. If the expression in theelse if
was true, the function wouldn't return a value.The following should fix it:
错误出在您的 palcheck 函数中,您忘记返回递归函数的值
The error is in your palcheck function, you forgot to return the value of the recursed function
需要
以防字母数量为偶数。
needs to be
in case there are an even number of letters.
该问题已在上面的
此处
标记。当选择这种情况时,您不会从函数中返回任何内容,这是一个问题。你可能应该使用:The problem is marked
here
above. When that case is selected, you're not returning anything from the function, which is a problem. You should probably use: