What does "error: non-void function does not return a value in all control paths." or "error: use of undeclared identifier 'sum'" means in c?
What does "error: non-void function does not return a value in all control paths." or "error: use of undeclared identifier 'sum'" means in c?
我一直在做一个程序。基本上是这样的:
// CODE A
#include <cs50.h>
#include <stdio.h>
int a (string b)
{
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number
}
return sum;
}
------------------------------------------------------
// CODE B
#include <cs50.h>
#include <stdio.h>
int a (string b)
{
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number
return sum;
}
}
所以当我 运行 代码 A 时,它给了我这个错误:
error: use of undeclared identifier 'sum' [Line 8]
所以我的倡议是将 return 和放在 for 循环中(如代码 B)并给我一个错误:
error: non-void function does not return a value in all control paths. [Line 7]
我不明白这是为什么。
第一个错误基本上意味着您 not 声明 sum
的方式使得函数 a
可以找到 sum
。一种选择是在循环开始之前定义 sum
(并将其初始化为 0
):int sum = 0;
.
在 CODE B
中,函数 return 是 int
。编译器说,如果从未执行 for
循环(因为在某些情况下条件第一次失败),您的函数不会指定 return
语句。
此外,在 CODE B
中,即使循环执行,由于 return
语句,它也只执行一次。
也许您想这样做:
int a (string b)
{
int sum = 0;
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number; // Assuming number is initialized
}
return sum; // This returns `sum` to the calling function
}
您可以通过以下操作捕获函数的 return 值:int x = a(some_string_argument);
如果 react-native/Expo 弹出 react native
,只需在 ExUserFacingNotificationsPermissionsRequester.m 中添加以下行
- (NSNumber *)authorizationStatusToEnum:(UNAuthorizationStatus)status
{
switch (status) {
case UNAuthorizationStatusNotDetermined:
return @(0);
case UNAuthorizationStatusDenied:
return @(1);
case UNAuthorizationStatusAuthorized:
return @(2);
case UNAuthorizationStatusProvisional:
return @(3);
+ default:
+ return @(3);
}
}
我一直在做一个程序。基本上是这样的:
// CODE A
#include <cs50.h>
#include <stdio.h>
int a (string b)
{
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number
}
return sum;
}
------------------------------------------------------
// CODE B
#include <cs50.h>
#include <stdio.h>
int a (string b)
{
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number
return sum;
}
}
所以当我 运行 代码 A 时,它给了我这个错误:
error: use of undeclared identifier 'sum' [Line 8]
所以我的倡议是将 return 和放在 for 循环中(如代码 B)并给我一个错误:
error: non-void function does not return a value in all control paths. [Line 7]
我不明白这是为什么。
第一个错误基本上意味着您 not 声明 sum
的方式使得函数 a
可以找到 sum
。一种选择是在循环开始之前定义 sum
(并将其初始化为 0
):int sum = 0;
.
在 CODE B
中,函数 return 是 int
。编译器说,如果从未执行 for
循环(因为在某些情况下条件第一次失败),您的函数不会指定 return
语句。
此外,在 CODE B
中,即使循环执行,由于 return
语句,它也只执行一次。
也许您想这样做:
int a (string b)
{
int sum = 0;
for (int i = 0, n = strlen(b); i < n; i++)
{
// Something
sum += number; // Assuming number is initialized
}
return sum; // This returns `sum` to the calling function
}
您可以通过以下操作捕获函数的 return 值:int x = a(some_string_argument);
如果 react-native/Expo 弹出 react native
,只需在 ExUserFacingNotificationsPermissionsRequester.m 中添加以下行- (NSNumber *)authorizationStatusToEnum:(UNAuthorizationStatus)status
{
switch (status) {
case UNAuthorizationStatusNotDetermined:
return @(0);
case UNAuthorizationStatusDenied:
return @(1);
case UNAuthorizationStatusAuthorized:
return @(2);
case UNAuthorizationStatusProvisional:
return @(3);
+ default:
+ return @(3);
}
}