Multiple Source Files with Structures

This is a discussion on Multiple Source Files with Structures within the C++ Programming forums, part of the General Programming Boards category; i have a big favour to ask, could sombody please give me an example of multiple source files using structures. ...

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

    Multiple Source Files with Structures

    i have a big favour to ask, could sombody please give me an example of multiple source files using structures. the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    i have tried before but kept getting "multiple definition" errors and a few others i cant remember, as i kept deleting the projects and adding to my other. i have managed to get functions working in different files before quite comfortably with functions, however i cant seem to manage it with structures.


    thanks,

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    this goes into the header
    Code:
    #ifndef STRUCT_EXAMPLE_H
    #define STRUCT_EXAMPLE_H
    
    typedef struct example_tag
    {
       int x;
    }example;
    
    int fillstruct(example* var);
    #endif
    This goes into the c-file
    Code:
    #include "struct_example.h"
    int fillstruct(example* var)
    {
       if(var) var->x = 5.0;
       return 0;
    }
    in this case you can have several c-files using the struct without the problem

    PS. hope you don't use global vars
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by vart
    this goes into the header
    Code:
    #ifndef STRUCT_EXAMPLE_H
    #define STRUCT_EXAMPLE_H
    
    typedef struct example_tag
    {
       int x;
    }example;
    
    int fillstruct(example* var);
    #endif
    This goes into the c-file
    Code:
    #include "struct_example.h"
    int fillstruct(example* var)
    {
       if(var) var->x = 5.0;
       return 0;
    }
    in this case you can have several c-files using the struct without the problem

    PS. hope you don't use global vars
    so the structure goes in the headerfile, and the rest in the .cpp file?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    > the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    This is not a large file by any means. So don't worry much with breaking it up. Unless of course, the file contains unrelated code. A good rule of thumb is to divide your source files so that each file contains code that is somehow related.

    > i have tried before but kept getting "multiple definition" errors and a few others i cant remember,

    Some things can make this happen. If you are trying to include a source file (.cpp, .c,...) into other sources files, for instance. Or if you are coding definitions inside a header file (.hpp, .h,...). Header files should not contain any definitions (with a few exceptions like class and structure definitions, iniline function definitions, const definitions).

    > however i cant seem to manage it with structures.

    Is it possible that you are defining your structures like this?

    Code:
    struct SomeStruct {
        /* ... */
    } foo;
    By doing it you are both defining the struct SomeStruct (which is valid inside an header file), and defining a variable called foo that is of type SomeStruct. That will create you problems when trying to include the header file in more than one source file. You will get a multiple-definitions error of foo.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    could you explain why it has typedef and i take it exmple_tag is the name of the structure?

  6. #6
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    Quote Originally Posted by Mario F.
    > the reason is that i have a rather large file with 1k+ lines and roughly 18 functions, all in 1 source file.

    This is not a large file by any means. So don't worry much with breaking it up. Unless of course, the file contains unrelated code. A good rule of thumb is to divide your source files so that each file contains code that is somehow related.

    > i have tried before but kept getting "multiple definition" errors and a few others i cant remember,

    Some things can make this happen. If you are trying to include a source file (.cpp, .c,...) into other sources files, for instance. Or if you are coding definitions inside a header file (.hpp, .h,...). Header files should not contain any definitions (with a few exceptions like class and structure definitions, iniline function definitions, const definitions).

    > however i cant seem to manage it with structures.

    Is it possible that you are defining your structures like this?

    Code:
    struct SomeStruct {
        /* ... */
    } foo;
    By doing it you are both defining the struct SomeStruct (which is valid inside an header file), and defining a variable called foo that is of type SomeStruct. That will create you problems when trying to include the header file in more than one source file. You will get a multiple-definitions error of foo.
    the reason is that it is tedious to work on as i have to keep scrolling up and down all the time, as im not an experienced programmer. and that its rather difficult to find the correct funtion i want to work on.

    i had my structure declared in the header file as

    Code:
    struct somestruct
    {
    int x;
    };
    
    somestruct mystruct[5];

  7. #7
    dac
    dac is offline
    Registered User
    Join Date
    Jan 2006
    Location
    North Yorkshire, England
    Posts
    147
    anyway it doesnt matter, ill just keep it in one source file.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by dac
    could you explain why it has typedef and i take it exmple_tag is the name of the structure?
    it's because I'm regular with using structs in C...
    there - you cannot use struct name without struct keyword if you don't typedef it previously...

    in C++ classes are mostly used...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutex across multiple source files
    By Quasar in forum Linux Programming
    Replies: 7
    Last Post: 12-04-2007, 07:25 AM
  2. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  3. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 02:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-03-2004, 11:19 PM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM

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