C++ 错误 "not all control paths return a value"

标签 c++

<分区>

我正在尝试通过调用函数来输出字符串的反转。我认为我的反向功能有问题。 我不断收到错误消息“并非所有控制路径都返回一个值。”

#include<iostream>

using std::cout;
using std::endl;
using std::cin;
int LengthofString(    char *); // declaring prototype for length of the string 
int ReverseD(char *);

int main()
{
    char string1[100];
    cout<<"Enter a string: ";
    cin>>string1;

    cout<<"Length of string is "<<LengthofString(string1)<<endl<<ReverseD(string1)<<endl;

    system("PAUSE");
    return 0;
}

int LengthofString(   char *x)
{
    int index;
    for(index = 0; *x!='\0';x++,index++);

    return index;
}

int reverse(char* y)
{
  int ii, n;
  n = LengthofString(y); 
  for(ii=0; ii<n/2;ii++) {
    char temp;
    temp = y[ii];
    y[ii] = y[n - ii - 1];
    y[n - ii] = temp;
    return temp;
  }
}

最佳答案

reverse 函数中,您在for 循环中return,如果满足条件则可能不会执行。尝试将 return 语句移到循环之外。

您的返回变量也在循环范围内声明,因此您也需要更改它,但这至少会修复您的原始错误。

关于C++ 错误 "not all control paths return a value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17176725/

相关文章:

c++ - 如何使用原始 Windows API 以编程方式将证书安装到用户的受信任发布者存储中?

c++ - 在创建 OpenCL 上下文之前设置线程亲和性会导致段错误

c++ - C++中的访问冲突

c++ - 未定义标识符/未声明

c++ - 运动代码c++

C++、Python、不兼容的数字类型

c++ - 删除非指针的通用列表

c++ - 为什么通过 weak_ptr 调用这么慢?

c++ - 标量类型上的 Eigen 模板无法编译固定大小的子矩阵操作

android - 为 Android 编译英特尔 TBB - ARMv7 ARMv8 MIPS