Вопрос:

Control reaches end of non-void function

c++ qt return

3594 просмотра

4 ответа

1835 Репутация автора

The following code snippet generates some warning messages when compiling:

Cluster& Myclass::getCluster(const Point &p)
{
    foreach (Cluster c, *this)
        foreach (Point point, c)
            if (point == p)
                return c;
}

The warnings are:

  1. reference to local variable 'c' returned [enabled by default]
  2. control reaches end of non-void function [when using -Wreturn-type]

I know that I am not returning a value if the condition fails. However, when I try return 0 it gave me error.

How can I solve these issues?

Автор: shofee Источник Размещён: 22.03.2012 09:28

Ответы (4)


1 плюс

319214 Репутация автора

The variable c is a local variable, as it's declared and defined inside the getCluster function. By returning a reference to it, when the function returns that reference "point" to where the variable used to be.

For the other warning, what if the condition is never true, then what does the function return?

Автор: Some programmer dude Размещён: 22.03.2012 09:35

1 плюс

170802 Репутация автора

reference to local variable 'c' returned [enabled by default]

You should not return a reference to a local variable because once the function is returned the variable does not exist anymore and hence you have reference to something that does not exist. The compiler warns you of this.

control reaches end of non-void function [-Wreturn-type]

Once you specify a return type for a function, every control path should return the value. If the condition evaluates to false then your code never returns anything hence the compiler complains.

Автор: Alok Save Размещён: 22.03.2012 09:35

6 плюса

13371 Репутация автора

Решение

If your function can legitimately fail to find a matching Cluster, then you should have it return a pointer:

Cluster* Myclass::getCluster(const Point &p)
{
    foreach (Cluster c, *this)
        foreach (Point point, c)
            if (point == p)
                return &c;
    return 0; // or return nullptr; in C++11
}

But this doesn't work yet, because c is a local variable. So you make it a reference, like this:

Cluster* Myclass::getCluster(const Point &p)
{
    foreach (Cluster& c, *this)
        foreach (Point point, c)
            if (point == p)
                return &c;
    return 0; // or "return nullptr;" in C++11
}
Автор: TonyK Размещён: 22.03.2012 09:36

2 плюса

195531 Репутация автора

The first means that c is a local variable. As such, it will go out of scope and die when the function returns. Since you are returning by reference, the caller gets a dangling reference. The other error is that you fail to return anything under certain conditions. Returning 0 doesn't help, because the return type is a reference to Cluster. You need some mechanism to ensure that you return a Cluster reference to a Cluster that doesn't die immediately. See for example this question.

Автор: juanchopanza Размещён: 22.03.2012 09:37
Вопросы из категории :
32x32