Thread: Quick Question on Multiple Source Files

  1. #16
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    #ifndef  MYHEADER_H_INCLUDED_
    #define  MYHEADER_H_INCLUDED_
    
    void Update_Program();// moved inside the guard
    void Load_Information();
    
    void Order_Quote();
    
    
    struct Part_Struct   //decleration of data structure for parts
    {
        int Part_ID;
        char Description[15];
        char Condition[10];
        float Guide_Price;
        int Origin_Vehicle_ID;
        bool Present;
    };
    
    struct Vehicle_Struct //decleration of data structure for vehicles
    {
        int Vehicle_ID;
        char Model[10];
        char Colour[10];
        int Mileage;
        float Purchase_Price;
        Part_Struct Parts[323];
    };
    
    struct Order_Struct  //decleration of data structure for orders
    {
        int Order_ID;
        int Customer_ID;
        int Part_ID[10];
        bool Paid;
    };
    
    struct Customer_Struct  //decleration of data structure for customers details
    {
           int Customer_ID;
           char Name[15];
           char Address[15];
           char Addresstwo[15];
           char Town[10];
           char County[10];
           char Postcode[6];
           char TelNo[12];
           Order_Struct Orders[10];
    };
    
    extern Customer_Struct Customers[10]; // declare extern, moved
    extern Vehicle_Struct Vehicles[10];// declare extern 
    extern int Number_of_Vehicles , Number_of_Customers;   // remove initialisation
    
    #endif
    EDIT: The arrays have to be in the main module as well

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Just a bit terminology:
    Code:
    void func(); // A function declaration.
    extern int i; // A variable declaration.
    struct s; // A type declaration.
    
    void func()
    {
    } // A function definition.
    
    int i; // A variable definition.
    
    struct s // A type definition.
    {
      int bla; // A member variable declaration.
    
      static int count; // A static member variable declaration.
    };
    
    int s::count; // A static member variable definition.
    
    s an_s; // Definition of a variable, instantiation of an s, implies definition of the bla member.
    These differences are important.
    Remember: you can have any amount of declarations for any name, as long as they're all the same, except for members: they may only be declared once. You can only have one definition for any variable or function in the entire program, except for the symbols that have internal linkage (the static keyword outside functions or classes). You can only have one definition per compilation unit (.cpp plus all its included headers) for any given type (class, enum, struct). Multiple definitions in multiple compilation units must be exactly the same. (Hardly any compiler actually checks this, but it can lead to really nasty runtime errors.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    im just getting loads of;

    " multiple definition of `Number_of_Customers' "

    " multiple definition of `Customers' "

    regarding both global vars and struct instances. but they are the only errors, the other have gone. so i think it could be just 1 last thing tbh.

  4. #19
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    int Customers
    should be put only once in one of the cpp-files

    Or you did it in several cpp-files, or in some header-file
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #20
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    >Never declare a variable in a header

    I agree, the only variable you can leagally declare in a header file is a static one.
    Usually after a class declaration. But I never follow this rule. Sperating declaration
    for implemetation is a golden key in sucsesful program matience. Darn, i just
    contradicted myself! LOL
    Double Helix STL

  6. #21
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    how often do you guys tend to use global variables anyway?

  7. #22
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by dac
    how often do you guys tend to use global variables anyway?
    I use them quite frequently. But I never show any of my sourcecode to anybody. That saves me from a lot of discussions about their usefullness.
    Kurt

  8. #23
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    In general, global variables should be avoided unless the programmer has a good
    reason to use one. In this case, using a static is just hte same, it lasts for the length
    of the programs life time and retains its value throughout function calls.
    Double Helix STL

  9. #24
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by swgh
    In general, global variables should be avoided unless the programmer has a good
    reason to use one. In this case, using a static is just hte same, it lasts for the length
    of the programs life time and retains its value throughout function calls.
    how do you guys think i could eliminate the global variables? the program is a for a car breakers (structs a bit of a give away lol), all 15 of the modules are local but i have 4 which arent, holding number of customers which is used by most modules same with vehicles, and also the data structs need to be used by each modules too.

  10. #25
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    how do you guys think i could eliminate the global variables?
    It is you, who should think about it.
    If you call some funtion you can pass everithing it needs as parameters instead of accessing globals
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to create a C project with multiple source files
    By MSF1981 in forum C Programming
    Replies: 5
    Last Post: 03-22-2009, 09:25 AM
  2. Working with multiple source files
    By abh!shek in forum C Programming
    Replies: 17
    Last Post: 06-03-2008, 11:23 AM
  3. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 08:25 AM
  4. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 09:07 AM
  5. 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