Thread: Variables in header files

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102

    Variables in header files

    My project consists of two source files and one header file.

    DX.cpp
    Code:
    #include "stdafx.h"
    //...
    case WM_LBUTTONDOWN:
                         i+=0.1;
                         break;
    //...
    graphics.cpp
    Code:
    #include "stdafx.h"
    //...
    D3DXMatrixRotationX(&matRotateX, i);
    //...
    stdafx.h
    Code:
    //...
    float i;
    //...
    The problem is that i need stdafx.h to be included in each of my source files, but that redefines i. Where should I put variables which I need in every souce file?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Where should I put variables which I need in every souce file?
    You can put the declarations for a variable in your header, but the definition should be placed in the implementation file:
    Code:
    //...
    extern float i;
    //...
    Code:
    #include "stdafx.h"
    //...
    float i;
    //...
    D3DXMatrixRotationX(&matRotateX, i);
    //...
    Of course, using a global name like i is flirting with disaster because it's such a common identifier. Also, stdafx.h is a well known header in Microsoft compilers, so it's unwise to reuse it.
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > float i;
    // put this in a header file
    extern float i;

    // put this in ONE source file
    float i;

    That is, if you insist on using global variables.

    Meh - beaten by the alphabet again
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    I am wondering, what makes "stdafx.h" so popular header file name?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what makes "stdafx.h" so popular header file name?
    It's the name of the file which VC++ uses to implement "precompiled headers".

    You put all the commonly used header files your project needs into stdafx.h
    You then #include "stdafx.h" as the first include in all your source files.

    Supposedly, it speeds up builds.
    I do know one thing though, it is the single biggest PITA as almost every single noob has a question about it at some point.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jan 2006
    Location
    Latvia
    Posts
    102
    Thanks guys, I can always rely on you. Now help me out with debugging

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confusion on header and source files
    By dnguyen1022 in forum C++ Programming
    Replies: 4
    Last Post: 01-17-2009, 03:42 AM
  2. C Header Files
    By devarishi in forum C Programming
    Replies: 8
    Last Post: 12-10-2008, 04:53 PM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. variables across files
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 07-23-2002, 10:58 AM
  5. Header files
    By Jez_Master in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2002, 07:56 AM