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:But this doesn’t work yet, because
cis a local variable. So you make it a reference, like this: