Control reaches end of non-void function

Hi forum,

I am running into an odd issue. G++ tells me a warning, warning: control reaches end of non-void function [-Wreturn-type].
Any thoughts? My code is below, and it seems to me that it will always return something...



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	bool Date::inRangeDay(int yy, int mm, int dd) const {
		if (13 > mm || 0 < mm) {
			if (mm == 2) {
				//leap year rules
				if (yy % 4 == 0) {
					if (yy % 100 == 0 && yy % 400 == 0) {
						return (1 <= dd && 29 >= dd) ? true : false;
					}
					else {
						return (1 <= dd && 29 >= dd) ? true : false;
					}
				}
				else {
					return (1 <= dd && 28 >= dd) ? true : false;
				}
			}
			else if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) {
				return (1 <= dd && 31 >= dd) ? true : false;
			}
			else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
				return (1 <= dd && 30 >= dd) ? true : false;
			}
		}
		else {
			return false;
		}
	}
You essentially have
1
2
3
4
5
6
IF A
  IF B THEN return
  ELSE IF C THEN return
  ELSE IF D THEN return
  ELSE ????
ELSE return

We do see that B, C or D will be true for all values for which A is true. The compiler does not calculate that and warns about the "impossible" path.

Consider
1
2
3
if (mm == 2)
else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)
else

The (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) is true on the else, is it not?
Hi

The if statement on line 3 has no else, even though this might logically never happen, the compiler sees it as a possible path.

But your code is too complex IMO. Consider having a IsLeapYear function - a one liner; and use a vector for the months and the number of days they have - the subscript is the month number.

Good Luck !!
keskiverto, what if month is an invalid 14?

I see what youre saying about the pseudocode, but I think I need to do it like this:

1
2
3
4
if (mm == 2)
else if (mm == 4 || mm == 6 || mm == 9 || mm == 11)
else if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12)
else


Does it sound feasible to you?

TheIdeasMan, we have not learned vectors yet, thank you for your input.
Edit

Got it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
	bool Date::inRangeDay(int yy, int mm, int dd) const {
		if (13 > mm || 0 < mm) {
			if (mm == 2) {
				//leap year rules
				if (yy % 4 == 0) {
					if (yy % 100 == 0 && yy % 400 == 0) {
						return (1 <= dd && 29 >= dd) ? true : false;
					}
					else {
						return (1 <= dd && 29 >= dd) ? true : false;
					}
				}
				else {
					return (1 <= dd && 28 >= dd) ? true : false;
				}
			}
			else if (mm == 4 || mm == 6 || mm == 9 || mm == 11) {
				return (1 <= dd && 30 >= dd) ? true : false;
			}
			else {
				return (1 <= dd && 31 >= dd) ? true : false;
			}
		}
		else {
			return false;
		}
	}
TheIdeasMan, we have not learned vectors yet, thank you for your input.



Well you could use an ordinary array then :+)

It beats me that people know about classes, but not basic STL containers :+)
Topic archived. No new replies allowed.