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:
- reference to local variable 'c' returned [enabled by default]
- 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?
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
}
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.
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?
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.
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:
- reference to local variable 'c' returned [enabled by default]
- 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?
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
}
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.
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?
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.
0 commentaires:
Enregistrer un commentaire