Warning when using an ENUM in a Function prototype

B

benn

Here's the setup...

Defines.h file contains:
enum DAY { monday, tueday };

DayFunctions.h contains prototype:
void printIsMonday ( enum DAY currentDay);

DayFunctions.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void printIsMonday ( enum DAY currentDay)
{
if (currentDay == monday)
printf("Monday!");
}

Main.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void main (void)
{
enum DAY eDayVariable;
eDayVariable = monday;
printIsMonday( eDayVariable);
}


The warning message the compiler gives for the prototype declaration
line in DayFunctions.h:
"
"enum DAY" declared inside parameter list. Its scope is only
definition or declartion, which is
probably not what you want. Parameter has incomplete type.
"

I tried removing the "enum" word from the prototype declaration, but
then it fails to compile because it
doesn't know what DAY is. What is the problem?!

Thanks!
 
K

Keith Thompson

benn said:
Here's the setup...

Defines.h file contains:
enum DAY { monday, tueday };

DayFunctions.h contains prototype:
void printIsMonday ( enum DAY currentDay);

DayFunctions.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void printIsMonday ( enum DAY currentDay)
{
if (currentDay == monday)
printf("Monday!");
}

Main.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void main (void)
{
enum DAY eDayVariable;
eDayVariable = monday;
printIsMonday( eDayVariable);
}


The warning message the compiler gives for the prototype declaration
line in DayFunctions.h:
"
"enum DAY" declared inside parameter list. Its scope is only
definition or declartion, which is
probably not what you want. Parameter has incomplete type.
"
[...]

DayFunctions.h refers to "enum DAY", but the declaration is not visible.

Add
#include "Defines.h"
to DayFunctions.h.

And fix your declaration of main; it's "int main(void)".
 
K

Keith Thompson

Pietro Cerutti said:
Keith said:
benn said:
Here's the setup...

Defines.h file contains:
enum DAY { monday, tueday };

DayFunctions.h contains prototype:
void printIsMonday ( enum DAY currentDay);

DayFunctions.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void printIsMonday ( enum DAY currentDay)
{
if (currentDay == monday)
printf("Monday!");
}

Main.c contains:

#include "Defines.h"
#include "DayFunctions.h"

void main (void)
{
enum DAY eDayVariable;
eDayVariable = monday;
printIsMonday( eDayVariable);
}


The warning message the compiler gives for the prototype declaration
line in DayFunctions.h:
"
"enum DAY" declared inside parameter list. Its scope is only
definition or declartion, which is
probably not what you want. Parameter has incomplete type.
"
[...]
DayFunctions.h refers to "enum DAY", but the declaration is not
visible.
Add
#include "Defines.h"
to DayFunctions.h

Isn't it so that both DayFunctions.h and Defines.h get included in the
same translation unit, making the enum visible from the T.U.?

Yes, probably so.

Ideally, each header should have a #include for any headers containing
declarations that it depends on, and each header should have include
guards so that its contents are only included once in each translation
unit. In the code the OP presented, a #include for "DayFunctions.h"
requires a preceding #include for "Defines.h". The posted code does
satisfy that requirement (which I didn't notice when I first read it),
but IMHO it's poor style -- and it's very likely, given that the OP is
getting that warning message, that this problem *does* occur in his
real code.
 
B

benn

Pietro Cerutti said:
Keith said:
Here's the setup...
Defines.h file contains:
enum  DAY  { monday, tueday };
DayFunctions.h contains prototype:
void printIsMonday ( enum DAY currentDay);
DayFunctions.c contains:
#include "Defines.h"
#include "DayFunctions.h"
void printIsMonday ( enum DAY currentDay)
{
      if (currentDay == monday)
        printf("Monday!");
}
Main.c contains:
#include "Defines.h"
#include "DayFunctions.h"
void main (void)
{
      enum DAY   eDayVariable;
      eDayVariable = monday;
      printIsMonday( eDayVariable);
}
The warning message the compiler gives for the prototype declaration
line in DayFunctions.h:
"
"enum DAY" declared inside parameter list.  Its scope is only
definition or declartion, which is
probably not what you want.  Parameter has incomplete type.
"
[...]
DayFunctions.h refers to "enum DAY", but the declaration is not
visible.
Add
    #include "Defines.h"
to DayFunctions.h
Isn't it so that both DayFunctions.h and Defines.h get included in the
same translation unit, making the enum visible from the T.U.?

Yes, probably so.

Ideally, each header should have a #include for any headers containing
declarations that it depends on, and each header should have include
guards so that its contents are only included once in each translation
unit.  In the code the OP presented, a #include for "DayFunctions.h"
requires a preceding #include for "Defines.h".  The posted code does
satisfy that requirement (which I didn't notice when I first read it),
but IMHO it's poor style -- and it's very likely, given that the OP is
getting that warning message, that this problem *does* occur in his
real code.

--
Keith Thompson (The_Other_Keith) (e-mail address removed)  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

Thanks, I always put include guards, and generally I thought it was
*not* good style to put include files inside of include files. I'll
look it up in Effective C++ (Myers) to see if its mentioned!

Including the Defines.h inside DayFunction.h did indeed eliminate the
warning, but why?? Both .c files include the Defines header file
first, and the compiler therefore know about the enum type before
tackling the DayFunction.h file.

Incidentally, if I remove the prototype all together from
DayFunction.h, then I get the (relatively benign) warning that
printIsMonday is being declared implicitly.
 
F

Flash Gordon

benn wrote, On 08/10/08 21:30:
Pietro Cerutti said:
Keith Thompson wrote:
Here's the setup...
Defines.h file contains:
enum DAY { monday, tueday };
DayFunctions.h contains prototype:
void printIsMonday ( enum DAY currentDay);
DayFunctions.c contains:
#include "Defines.h"
#include "DayFunctions.h"
void printIsMonday ( enum DAY currentDay)
{
if (currentDay == monday)
printf("Monday!");
}
Main.c contains:
#include "Defines.h"
#include "DayFunctions.h"
void main (void)
{
enum DAY eDayVariable;
eDayVariable = monday;
printIsMonday( eDayVariable);
}
The warning message the compiler gives for the prototype declaration
line in DayFunctions.h:
"
"enum DAY" declared inside parameter list. Its scope is only
definition or declartion, which is
probably not what you want. Parameter has incomplete type.
"
[...]
DayFunctions.h refers to "enum DAY", but the declaration is not
visible.
Add
#include "Defines.h"
to DayFunctions.h
Isn't it so that both DayFunctions.h and Defines.h get included in the
same translation unit, making the enum visible from the T.U.?
Yes, probably so.

Ideally, each header should have a #include for any headers containing
declarations that it depends on, and each header should have include
guards so that its contents are only included once in each translation
unit. In the code the OP presented, a #include for "DayFunctions.h"
requires a preceding #include for "Defines.h". The posted code does
satisfy that requirement (which I didn't notice when I first read it),
but IMHO it's poor style -- and it's very likely, given that the OP is
getting that warning message, that this problem *does* occur in his
real code.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Please don't quote peoples signatures, the bit typically after the "-- "
unless you are commenting on them.
Thanks, I always put include guards, and generally I thought it was
*not* good style to put include files inside of include files. I'll
look it up in Effective C++ (Myers) to see if its mentioned!

Why look in a C++ book to find out what is good style for C? Wouldn't
looking in a C book make more sense?
Including the Defines.h inside DayFunction.h did indeed eliminate the
warning, but why??

Because you code is not the same as the code you posted. I suggest
looking on line 42 for the answer.
Both .c files include the Defines header file
first, and the compiler therefore know about the enum type before
tackling the DayFunction.h file.

Either there is a bug in the compiler, which is *very* unlikely, or
there is a bug in your code.
Incidentally, if I remove the prototype all together from
DayFunction.h, then I get the (relatively benign) warning that
printIsMonday is being declared implicitly.

That is a far from benign warning (in general rather than in this
specific instance), it is a warning that indicates a potentially serious
problem that in some cases on some systems can cause the program to crash.
 
R

Richard Bos

Pietro Cerutti said:
Isn't it so that both DayFunctions.h and Defines.h get included in the
same translation unit, making the enum visible from the T.U.?

I suspect that in his real code, most files do have Defines.h #included
before DayFunctions.h, but in one file, he's either forgotten to add
Defines.h, or has them in the wrong order.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top