Warning when using an ENUM in a Function prototype

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • benn

    Warning when using an ENUM in a Function prototype

    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!
  • Pietro Cerutti

    #2
    Re: Warning when using an ENUM in a Function prototype

    benn 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 "
    #include <stdio.h>
    >
    void printIsMonday ( enum DAY currentDay)
    {
    if (currentDay == monday)
    printf("Monday! ");
    }
    >
    Main.c contains:
    >
    #include "Defines.h"
    #include "DayFunctions.h "
    #include <stdio.h>
    >
    void main (void)
    int main (void)
    {
    enum DAY eDayVariable;
    eDayVariable = monday;
    printIsMonday( eDayVariable);
    putc('\n', stdout);
    return (EXIT_SUCCESS);
    }
    Then your code is ok.
    >
    >
    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 think you have mispelled the name DAY in your _actual_ code (i.e., not
    the one you have posted). The code that you have posted compiles fine here.
    >
    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!

    --
    Pietro Cerutti

    Comment

    • Keith Thompson

      #3
      Re: Warning when using an ENUM in a Function prototype

      benn <benn686@hotmai l.comwrites:
      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)".

      --
      Keith Thompson (The_Other_Keit h) kst-u@mib.org <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"

      Comment

      • Pietro Cerutti

        #4
        Re: Warning when using an ENUM in a Function prototype

        Keith Thompson wrote:
        benn <benn686@hotmai l.comwrites:
        >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.?

        [snip "fix main declaration"]

        --
        Pietro Cerutti

        Comment

        • Pietro Cerutti

          #5
          Re: Warning when using an ENUM in a Function prototype

          Keith Thompson wrote:
          benn <benn686@hotmai l.comwrites:
          >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.?

          [snip "fix main declaration"]

          --
          Pietro Cerutti

          Comment

          • Pietro Cerutti

            #6
            Re: Warning when using an ENUM in a Function prototype

            Pietro Cerutti wrote:
            Keith Thompson wrote:
            >benn <benn686@hotmai l.comwrites:
            >>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.?
            Sorry for the double post, giganews is not behaving nicely to me today...
            >
            [snip "fix main declaration"]
            >

            --
            Pietro Cerutti

            Comment

            • Keith Thompson

              #7
              Re: Warning when using an ENUM in a Function prototype

              Pietro Cerutti <gahr_SPAM_gahr _ME_chwrites:
              Keith Thompson wrote:
              >benn <benn686@hotmai l.comwrites:
              >>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_Keit h) kst-u@mib.org <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"

              Comment

              • benn

                #8
                Re: Warning when using an ENUM in a Function prototype

                On Oct 8, 12:42 pm, Keith Thompson <ks...@mib.orgw rote:
                Pietro Cerutti <gahr_SPAM_gahr _ME_chwrites:
                Keith Thompson wrote:
                benn <benn...@hotmai l.comwrites:
                >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_Keit h) ks...@mib.org  <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.



                Comment

                • Flash Gordon

                  #9
                  Re: Warning when using an ENUM in a Function prototype

                  benn wrote, On 08/10/08 21:30:
                  On Oct 8, 12:42 pm, Keith Thompson <ks...@mib.orgw rote:
                  >Pietro Cerutti <gahr_SPAM_gahr _ME_chwrites:
                  >>Keith Thompson wrote:
                  >>>benn <benn...@hotmai l.comwrites:
                  >>>>Here's the setup...
                  >>>>Defines.h file contains:
                  >>>>enum DAY { monday, tueday };
                  >>>>DayFunction s.h contains prototype:
                  >>>>void printIsMonday ( enum DAY currentDay);
                  >>>>DayFunction s.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
                  >>>>definitio n 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_Keit h) ks...@mib.org <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.
                  --
                  Flash Gordon
                  If spamming me sent it to smap@spam.cause way.com
                  If emailing me use my reply-to address
                  See the comp.lang.c Wiki hosted by me at http://clc-wiki.net/

                  Comment

                  Working...