Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. #include directive in switch case if clause

#include directive in switch case if clause

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.

    case ID_ONE:
    if (id == 1)
    {
    #include "Func.h"
    Func();
    }
    if (id == 2)
    {
    Func();
    }
    break;

    I get the following error:

    incompatible implicit declaration of function 'Func'

    If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?

    L J C J 4 Replies Last reply
    0
    • E elelont2

      Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.

      case ID_ONE:
      if (id == 1)
      {
      #include "Func.h"
      Func();
      }
      if (id == 2)
      {
      Func();
      }
      break;

      I get the following error:

      incompatible implicit declaration of function 'Func'

      If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You did not mention which line produced the error; I would guess the second one, which does not 'see' the include file. You need to move the ``#include`` statement outside the ``if`` clause.

      1 Reply Last reply
      0
      • E elelont2

        Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.

        case ID_ONE:
        if (id == 1)
        {
        #include "Func.h"
        Func();
        }
        if (id == 2)
        {
        Func();
        }
        break;

        I get the following error:

        incompatible implicit declaration of function 'Func'

        If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        This won't work. Include statements are processed by the C/C++ preprocessor[^] which is executed as first build step before compilation. But the if condition is processed when your application is executed. It is possible to use include statements somewhere inside the code. But then the file content must be valid code only (imagine what the statement does: It replaces the statement with the file content).

        1 Reply Last reply
        0
        • E elelont2

          Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.

          case ID_ONE:
          if (id == 1)
          {
          #include "Func.h"
          Func();
          }
          if (id == 2)
          {
          Func();
          }
          break;

          I get the following error:

          incompatible implicit declaration of function 'Func'

          If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          It is legal but probably doesn't do what is expected (by you). Suppose "Func.h" is

          double Func();

          Then, what you get (after preprocessor pass) is:

          case ID_ONE:
          if (id == 1)
          {
          double Func();
          Func(); // here Func is explicitely declared (returning a double value)
          }
          if (id == 2)
          {
          Func(); // here Func is implicitely declared (returning a int value)
          }

          1 Reply Last reply
          0
          • E elelont2

            Hi, i have a tool which generates some code, so i cannot place the include at the top of the file.

            case ID_ONE:
            if (id == 1)
            {
            #include "Func.h"
            Func();
            }
            if (id == 2)
            {
            Func();
            }
            break;

            I get the following error:

            incompatible implicit declaration of function 'Func'

            If i remove the #include then it compiles with a warning (implicit declaration). Is it not legal to #include in if statements?

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            elelont2 wrote:

            i have a tool which generates some code

            I question that statement. Certainly the professional code generation tools that I have used allow for customization which would always include a way to provide includes. If it an in-house tool then obviously the way to go would be to modify it. However an alternative would be to create your own simple tool which does nothing but insert a header. It is probably possible to do this with existing tools and an appropriate command line script. Even simple file concatenation might be sufficient. Then once you have that methodology down modify your build to do that step after code generation but before the compile.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups