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. HEADER FILE PROBLEM

HEADER FILE PROBLEM

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
5 Posts 4 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.
  • C Offline
    C Offline
    Caoimh
    wrote on last edited by
    #1

    I've two global variables which I require defined in a header file, unfortunately I require this file to be included in more than one C++ file. However I get an error in relation to the global variables being defined twice. Any suggestions on a workaround? Kind Regards Caoimh

    _ B P 3 Replies Last reply
    0
    • C Caoimh

      I've two global variables which I require defined in a header file, unfortunately I require this file to be included in more than one C++ file. However I get an error in relation to the global variables being defined twice. Any suggestions on a workaround? Kind Regards Caoimh

      _ Offline
      _ Offline
      _Magnus_
      wrote on last edited by
      #2

      Put the definition in the .cpp ex. .cpp int variable; .h extern int variable; /Magnus


      - I don't necessarily agree with everything I say

      1 Reply Last reply
      0
      • C Caoimh

        I've two global variables which I require defined in a header file, unfortunately I require this file to be included in more than one C++ file. However I get an error in relation to the global variables being defined twice. Any suggestions on a workaround? Kind Regards Caoimh

        B Offline
        B Offline
        Balkrishna Talele
        wrote on last edited by
        #3

        u forgot to type "extern" some.h int gGlobal; fx.c extern int gGlobal;

        1 Reply Last reply
        0
        • C Caoimh

          I've two global variables which I require defined in a header file, unfortunately I require this file to be included in more than one C++ file. However I get an error in relation to the global variables being defined twice. Any suggestions on a workaround? Kind Regards Caoimh

          P Offline
          P Offline
          Prakash Nadar
          wrote on last edited by
          #4

          We normally dont define a variable in the header file as the header file is included in many cpp files, it causes redefination of the variable. Instead we define the variable in a .cpp file or .c file and an extern in the .h file. In windows application developed using the project wizard, it creates two files namely stdafx.h and stdafx.cpp stdafx is by default included in each file that you add using the classwizard. and stdafx.cpp is generally a empty file except for #include "stdafx.h" So the best thing is to put the defination of the variable in the stdafx.cpp file and extern in stdafx.h file forexample in stdafx.cpp file

          #include "stdafx.h"
          int g_SomeGlobalVariable;

          and in stdafx.h file

          extern int g_SomeGlobalVariable;

          hope this solves ur problem.


          1.Why do people not wearing a wrist watch look at their wrist for time when people ask for time. 2.Why do people ask for time from people who are not wearing a wrist watch. Prakash, India.

          C 1 Reply Last reply
          0
          • P Prakash Nadar

            We normally dont define a variable in the header file as the header file is included in many cpp files, it causes redefination of the variable. Instead we define the variable in a .cpp file or .c file and an extern in the .h file. In windows application developed using the project wizard, it creates two files namely stdafx.h and stdafx.cpp stdafx is by default included in each file that you add using the classwizard. and stdafx.cpp is generally a empty file except for #include "stdafx.h" So the best thing is to put the defination of the variable in the stdafx.cpp file and extern in stdafx.h file forexample in stdafx.cpp file

            #include "stdafx.h"
            int g_SomeGlobalVariable;

            and in stdafx.h file

            extern int g_SomeGlobalVariable;

            hope this solves ur problem.


            1.Why do people not wearing a wrist watch look at their wrist for time when people ask for time. 2.Why do people ask for time from people who are not wearing a wrist watch. Prakash, India.

            C Offline
            C Offline
            Caoimh
            wrote on last edited by
            #5

            Thank You

            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