#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Minneapolis, MN
    Posts
    5
    Rep Power
    0

    How do I use a struct defined in a seperate .cpp file?


    Hello,

    I am trying to develop a library file that will be self-contained as much as possible...

    so far, the only EXTERNAL reference I need to make is to a generic structure which would be defined in whichever main program is using the library...

    ex:
    Code:
    ////////////////////////
    //main.h
    ///////////////////////
    
    // standard includes, misc code...
    
    struct Options
    {
    	string	s_param;
    	int	i_param;
    	bool	b_helpMd;
    	bool	b_debugMd;
    }; opt_obj
    From within my proj_lib.h file, I am declaring this struct as extern, like so:
    Code:
    ////////////////////////
    //proj_lib.h
    ///////////////////////
    
    extern struct Options opt_obj;
    This is not working though...

    I am trying to make this as 'pluggable' as possible, so that if I want to use this library in various programs, all I would need to do would be to #include it from within my main program header and create a struct object for it to utilize...

    can this be done, or is there a better way of doing it?

    Thanks!

    Joshua B.
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,625
    Rep Power
    4247
    Put the struct in your proj_lib.h file like so:
    Code:
    #ifndef __PROJ_LIB_H__
    #define __PROJ_LIB_H__
    
    struct Options
    {
    	string	s_param;
    	int	i_param;
    	bool	b_helpMd;
    	bool	b_debugMd;
    }; 
    
    extern struct Options opt_obj;
    
    #endif
    Then within main.cpp and proj_lib.cpp, you can do:
    #include "proj_lib.h"


    Now, you can put the compiled code of proj_lib.cpp separately into a library. Any other program that needs to use that lib, simply needs to #include "proj_lib.h" to get the structure definition and/or opt_obj. Hope this helps :)
    Last edited by Scorpions4ever; May 12th, 2003 at 08:45 PM.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Minneapolis, MN
    Posts
    5
    Rep Power
    0
    Hey scorpian, thanks for your help...

    but actually, I was hoping there might be a way to do this so that I could avoid putting the struct definition within the library file...

    what I would like to do (but don't know if it is possible) is make a self-contained .lib file which I could then link to any project I'm working on, and from program to program I could define any changeable run-tine options within the a structrue named Options placed in whatever main header is being used...

    If I put the struct within my library file, then I can not edit or modify the option choices without manually going in and changing the library itself...

    I don't know.. maybe I'm trying to do something that isn't normally done, or maybe I'm going about it the wrong way... this is only my first semester in C++, so anythings possible...

    Anyhow, thanks again for the help!!

    Joshua B.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,155
    Rep Power
    2222
    Originally posted by jbarker28
    Hey scorpian, thanks for your help...

    but actually, I was hoping there might be a way to do this so that I could avoid putting the struct definition within the library file...

    ...
    There's a little trick I learned from C++ that may also work in C (oops, I just verified that you are talking C++). However, for it to work the other modules that use the struct cannot directly access its fields.

    The idea is to make the extern variable a pointer to the struct. In C++, you can forward-declare a struct name then declare a pointer to that type of struct without having to worry about what's in that struct.

    Usually, we would use it to include an object in another class without having to include that class's header file, because to do so would create a circular-reference mess. Here's an example header file for your case off the top of my head:
    Code:
    #ifndef _HEADER_H_
    #define _HEADER_H_
    
    // the forward declaration, just to establish the existence of a struct with that tag name.
    
    struct aStruct;
    
    // you establish that aPtr is a pointer to that struct
    extern aStruct *aPtr;
    
    #endif
    aPtr will be initialized in your library code, which will be the only code module that needs to know the gory details about the struct. This is a desirable feature of OOP called "data hiding". And if other modules need access to certain fields within the struct, then write access methods for that.

    That's right, you can treat the struct like you would a class. You can even write constructors and destructors for it. To my knowledge and from my own experience, a struct is roughly (or not so roughly) equivalent to a public class. Inheritance might be restricted, but I'm not sure about that.

    Does it make sense and does it look like what you're looking for?
    Last edited by dwise1_aol; May 13th, 2003 at 12:06 AM.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    Minneapolis, MN
    Posts
    5
    Rep Power
    0
    That seems like a very interesting work-around...

    I'll give it a try and let you guy's know how it works out...

    Thanks :D

    Joshua B.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    15
    That's right, you can treat the struct like you would a class. You can even write constructors and destructors for it. To my knowledge and from my own experience, a struct is roughly (or not so roughly) equivalent to a public class. Inheritance might be restricted, but I'm not sure about that.
    In C++, the only difference between a class and a struct is the default access specifier.
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2003
    Posts
    11
    Rep Power
    0
    Originally posted by dwise1_aol
    There's a little trick

    #ifndef _HEADER_H_
    #define _HEADER_H_

    // the forward declaration, just to establish the existence of a struct with that tag name.

    struct aStruct;

    // you establish that aPtr is a pointer to that struct
    extern aStruct *aPtr;

    #endif
    [/code]
    It is realy trick.
    I prefer insert global objects in some common place.
    For example, in the App cpp file in the MFC project.

IMN logo majestic logo threadwatch logo seochat tools logo