Thread: Struct global?

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    1
    Rep Power
    0

    Struct global?


    I've made up a struct

    struct castice
    {
    double x, y, v_x, v_y, a_x, a_y, m;

    }objekt[2];

    but my code:block shows error during compilation when I try to use for example objekt[i].x in external function.

    What's wrong ?
    I guess the struct is global, right?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,900
    Rep Power
    481
    Your struct isn't global. I didn't know about it. Spread the word! Like this:

    file BS.h
    Code:
    #ifndef BALLISTIC_SIMULATION
    #define BALLISTIC_SIMULATION
    
    struct castice {
      double x, y, v_x, v_y, a_x, a_y, m;
    };
    
    #endif
    Then in all files that need struct castice
    Code:
    #include "BS.h"
    
    struct castice objekt[2];
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    Assuming you mean that you have two or more source files and that the "external function" is in the other source file.

    Did you extern objekt in that other source file that's trying to use it? extern's are usually placed in a header file and #include'd into the source files that need what's in it.

    PS
    Of course, as described in Msg#2, you also need to place that struct declaration in the header file.

    Remember, in a multiple-file project, every source file is compiled separately and independently with no knowledge about the compilation of any other source file. The only way for the compiler to know anything about what's in another source file is by being told through a header file.
    Last edited by dwise1_aol; May 7th, 2013 at 09:31 AM.

IMN logo majestic logo threadwatch logo seochat tools logo