C警告“返回”没有值,在函数中返回非空

时间:2021-05-22 20:03:49

I have this warning.

我有这个警告。

warning : 'return' with no value, in function returning non-void.

警告:“返回”没有值,在函数中返回非空。

3 个解决方案

#1


12  

You have something like:

你有:

int function(void)
{
    return;
}

Add a return value, or change the return type to void.

添加一个返回值,或者将返回类型更改为void。

The error message is very clear:

错误信息非常清楚:

warning : 'return' with no value, in function returning non-void.

警告:“返回”没有值,在函数中返回非空。

A return with no value is similar to what I showed. The message also tells you that if the function returns 'void', it would not give the warning. But because the function is supposed to return a value but your 'return' statement didn't, you have a problem.

没有值的返回与我展示的类似。消息还告诉您,如果函数返回“void”,则不会给出警告。但是因为这个函数应该返回一个值,而你的“return”语句没有返回,所以你有一个问题。

This is often indicative of ancient code. In the days before the C89 standard, compilers did not necessarily support 'void'. The accepted style was then:

这通常表示古代的代码。在C89标准之前,编译器并不一定支持“void”。当时的流行风格是:

function_not_returning_an_explicit_value(i)
char *i;
{
    ...
    if (...something...)
        return;
}

Technically, the function returns an int, but no value was expected. This style of code elicits the warning you got - and C99 officially outlaws it, but compilers continue to accept it for reasons of backwards compatibility.

从技术上讲,该函数返回一个int,但不期望有任何值。这种风格的代码会引起你的警告——而C99正式地禁止了它,但是编译器继续接受它是因为向后兼容的原因。

#2


4  

This warning also happens if you forget to add a return statement as the last statement:

如果您忘记将return语句添加为最后一个语句,那么也会发生此警告:

int func(){}

If you don't specify the return type of a function it defaults to int not to void so these are also errors:

如果你没有指定函数的返回类型,它默认为int而不是void,所以这些也是错误:

func(){}
func(){ return; }

If you really do not need to return a value you should declare your function as returning void:

如果您真的不需要返回值,您应该声明您的函数为返回void:

void func(){}
void func(){ return; }

#3


3  

This warning happens when you do this:

当你这样做时,这个警告就会出现:

int t() { return; }

Because t() is declared to return an int, but the return statement isn't returning an int. The correct version is:

因为t()被声明为返回int,但是返回语句没有返回int,正确的版本是:

int t() { return 0; }

Obviously your code is more complicated, but it should be fairly easy to spot a bare return in your code.

显然,您的代码更加复杂,但是在代码中发现一个简单的返回应该相当容易。

#1


12  

You have something like:

你有:

int function(void)
{
    return;
}

Add a return value, or change the return type to void.

添加一个返回值,或者将返回类型更改为void。

The error message is very clear:

错误信息非常清楚:

warning : 'return' with no value, in function returning non-void.

警告:“返回”没有值,在函数中返回非空。

A return with no value is similar to what I showed. The message also tells you that if the function returns 'void', it would not give the warning. But because the function is supposed to return a value but your 'return' statement didn't, you have a problem.

没有值的返回与我展示的类似。消息还告诉您,如果函数返回“void”,则不会给出警告。但是因为这个函数应该返回一个值,而你的“return”语句没有返回,所以你有一个问题。

This is often indicative of ancient code. In the days before the C89 standard, compilers did not necessarily support 'void'. The accepted style was then:

这通常表示古代的代码。在C89标准之前,编译器并不一定支持“void”。当时的流行风格是:

function_not_returning_an_explicit_value(i)
char *i;
{
    ...
    if (...something...)
        return;
}

Technically, the function returns an int, but no value was expected. This style of code elicits the warning you got - and C99 officially outlaws it, but compilers continue to accept it for reasons of backwards compatibility.

从技术上讲,该函数返回一个int,但不期望有任何值。这种风格的代码会引起你的警告——而C99正式地禁止了它,但是编译器继续接受它是因为向后兼容的原因。

#2


4  

This warning also happens if you forget to add a return statement as the last statement:

如果您忘记将return语句添加为最后一个语句,那么也会发生此警告:

int func(){}

If you don't specify the return type of a function it defaults to int not to void so these are also errors:

如果你没有指定函数的返回类型,它默认为int而不是void,所以这些也是错误:

func(){}
func(){ return; }

If you really do not need to return a value you should declare your function as returning void:

如果您真的不需要返回值,您应该声明您的函数为返回void:

void func(){}
void func(){ return; }

#3


3  

This warning happens when you do this:

当你这样做时,这个警告就会出现:

int t() { return; }

Because t() is declared to return an int, but the return statement isn't returning an int. The correct version is:

因为t()被声明为返回int,但是返回语句没有返回int,正确的版本是:

int t() { return 0; }

Obviously your code is more complicated, but it should be fairly easy to spot a bare return in your code.

显然,您的代码更加复杂,但是在代码中发现一个简单的返回应该相当容易。