“并非所有控制路径都返回值”

发布于 2024-10-21 07:27:32 字数 1325 浏览 3 评论 0原文

**编辑:除了下面指出的错误之外,我还错误地尝试根据此错误代码将其编译为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

表情可笑 2024-10-28 07:27:32

错误出现在您的 palcheck 函数中。如果 else if 中的表达式为 true,则该函数不会返回值。

以下应该修复它:

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;
}

The error is in your palcheck function. If the expression in the else if was true, the function wouldn't return a value.

The following should fix it:

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;
}
明媚殇 2024-10-28 07:27:32

错误出在您的 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;
}

The error is in your palcheck function, you forgot to return the value of the recursed function

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;
}
瀞厅☆埖开 2024-10-28 07:27:32
if (first == last)

需要

if (first >= last)

以防字母数量为偶数。

if (first == last)

needs to be

if (first >= last)

in case there are an even number of letters.

提笔落墨 2024-10-28 07:27:32
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
}

该问题已在上面的此处标记。当选择这种情况时,您不会从函数中返回任何内容,这是一个问题。你可能应该使用:

  return palcheck(word, first+1, last-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
}

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:

  return palcheck(word, first+1, last-1);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文