Quick Question on Multiple Source Files

This is a discussion on Quick Question on Multiple Source Files within the C++ Programming forums, part of the General Programming Boards category; ..ive divided my programs functions into multiple source files using a header file also, however im using global variables and ...

  1. #1
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147

    Quick Question on Multiple Source Files

    ..ive divided my programs functions into multiple source files using a header file also, however im using global variables and data structures (struct command) and the functions show compiler errors indicating that the structures and global variables have not been defined. so does anybody know where i should declare them? as they dont appear to be working when declared in the main.





    thanks,

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The declaration of the structs have to be put into the header to make them available in different modules.
    To be able to access the global variables of your main module from other modules you have to declare them in a header as extern as well.
    Kurt

  3. #3
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by ZuK
    The declaration of the structs have to be put into the header to make them available in different modules.
    To be able to access the global variables of your main module from other modules you have to declare them in a header as extern as well.
    Kurt
    ive declared my global variables and structures in the header file but im still getting the 'undeclared' error message.

  4. #4
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    ok its sorted now, forgot to include the header file in the functions source file. durrr...


    thanks Zuk.

  5. #5
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    ok, ive got a new problem. its now saying ive got multiple definition of everything declared globally in the header file. what could be causing this?

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I told you.

    e.g. in main.cc

    Code:
    int global_int;
    in the heder
    Code:
    extern int global_int;
    Never declare a variable in a header.
    Kurt

  7. #7
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by ZuK
    I told you.

    e.g. in main.cc

    Code:
    int global_int;
    in the heder
    Code:
    extern int global_int;
    Never declare a variable in a header.
    Kurt
    sorry if i seem pig headed Kurt, lol. do i put "extern" in front of the structs as well in the header? cause im getting a "27 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\HeaderFile.h `extern' can only be specified for objects and functions " error message.

    thanks,

  8. #8
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    No you don't. These structs are only declarations.
    But you should put include guards around your declarations in the header to protect them from multiple inclusions.
    like

    Code:
    #ifndef  MYHEADER_H_INCLUDED_
    #define  MYHEADER_H_INCLUDED_
    
    // the declarations 
    
    #endif
    Kurt

  9. #9
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    ive done what you said, now im getting "7 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\main.cpp In file included from main.cpp" error message. im not sure what this one means tbh.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Paste all of the error message(s), that is just the first part.

  11. #11
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    7 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\main.cpp In file included from main.cpp

    68 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\HeaderFile.h [Warning] `Number_of_Vehicles' initialized and declared `extern'

    68 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\HeaderFile.h [Warning] `Number_of_Customers' initialized and declared `extern'

    11 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\main.cpp redefinition of `int Number_of_Vehicles'

    68 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\HeaderFile.h `int Number_of_Vehicles' previously defined here

    11 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\main.cpp redefinition of `int Number_of_Customers'

    68 C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\HeaderFile.h `int Number_of_Customers' previously defined here

    C:\Documents and Settings\Dean.Downstairs\My Documents\Module 4\Makefile.win [Build Error] [main.o] Error 1

    thats all of them.

  12. #12
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    the extern declarations must not have any initialisation.
    guess you have to post a minimal example of what you have actually put into the header and into main.
    Kurt
    Last edited by ZuK; 12-01-2006 at 11:17 AM.

  13. #13
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by ZuK
    the extern declarations must not have any initialisation.
    Kurt
    ive got one set in the main as normal, and a duplicate set in the header file with extern in front of them.

    do the instances of the structures go in to the decleration guards?

  14. #14
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    everyting in the header should be inside the guard.
    in the .cc
    Code:
    int gvar = 0;   // here it is initialized
    in the header
    Code:
    extern int gvar; // no initialisation
    Kurt

  15. #15
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    in the main.

    Code:
    #include "HeaderFile.h"
    
    using namespace std;
    
    int Number_of_Vehicles = 0, Number_of_Customers = 0;
    most of the header file.

    Code:
    void Update_Program();//*
    void Load_Information();
    
    void Order_Quote();
    
    #ifndef  MYHEADER_H_INCLUDED_
    #define  MYHEADER_H_INCLUDED_
    
    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];
    };
    
    Customer_Struct Customers[10]; //arrayed data structures
    Vehicle_Struct Vehicles[10];
    
    #endif
    
    extern int Number_of_Vehicles = 0, Number_of_Customers = 0;

Page 1 of 2 12 LastLast
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, 07:25 AM
  4. pseudocode for multiple source files
    By Calef13 in forum C++ Programming
    Replies: 4
    Last Post: 11-13-2007, 08: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, 10:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21