December 21, 2010

Jonathan Jonathan
Lab Rat
50 posts

Why does this code get through the compiler without a return statement?

 

This must be a real newbie question.

I have this method declaration in the header (dbaccess.h):

  1. static QStringList indexProperties();

and this definition in dbaccess.cpp:

  1. QStringList DBAccess::indexProperties()
  2. {
  3. }

ie I’ve failed to provide return a value. I’ve just spent ages attempting to debug the problem, before spotting the obvious. Why doesn’t the compiler pick up the missing return?

7 replies

December 21, 2010

Volker Volker
Ant Farmer
5428 posts

With gcc you only get a warning:

  1. dbaccess.cpp:91: warning: control reaches end of non-void function

Visual Studio emit something similar if I remember correctly.

December 21, 2010

Gerolf Gerolf
Robot Herder
3253 posts

VS throws an error.

  1. error C4716: 'AAA::BBB' : must return a value

Just live tested with VS2008 SP1

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

December 21, 2010

florent.revelut florent.reve..
Lab Rat
103 posts

Obviously depends on the compiler… With gcc, don’t forget to set -Wall
VS2008 does it out of the box

December 21, 2010

Jonathan Jonathan
Lab Rat
50 posts

Yup, I’m using the MinGW tool chain under Windows 7.

Live and learn.

December 21, 2010

Volker Volker
Ant Farmer
5428 posts

You can turn warnings into errors in gcc with -Werror switch. You can enable this by adding this to your .pro file:

  1. QMAKE_CXXFLAGS += -Werror

December 22, 2010

Franzk Franzk
Lab Rat
831 posts

florent.revelut wrote:
VS2008 does it out of the box
Depends on your warning level just like with gcc, if I recall correctly.

 Signature 

“Horse sense is the thing a horse has which keeps it from betting on people.”—W.C. Fields

http://www.catb.org/~esr/faqs/smart-questions.html

December 22, 2010

Gerolf Gerolf
Robot Herder
3253 posts

Franzk wrote:
florent.revelut wrote:
VS2008 does it out of the box
Depends on your warning level just like with gcc, if I recall correctly.

Hi,

In MSVS it’s an error not a warning. I always use default levels: Warning level 3, no warning —> error translation. The MS compilers throw errors for such constructs.

 Signature 

Nokia Certified Qt Specialist.
Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

 
  ‹‹ Qt Network Programming      How to check whether the windows are already open.. ››

You must log in to post a reply. Not a member yet? Register here!