Thread: Multiple Source Files with Structures

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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];

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

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, 08: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, 03:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 05:47 PM