multi file programs and structs

This is a discussion on multi file programs and structs within the C++ Programming forums, part of the General Programming Boards category; Hi, I am trying to work on my first cpp assignment which requires me to break the code into multiple ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    45

    multi file programs and structs

    Hi,
    I am trying to work on my first cpp assignment which requires me to break the code into multiple files. I also have to work with structures. I am a little confused as to where my struct has to be defined. Should it be defined in a seperate .h file and included in all other files? or should the struct be defined in a .cpp file since it is actual definition? if so how do other files see it? I request anyone to tell me how to proceed.

    thanks,
    livin

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by livin
    Should it be defined in a seperate .h file and included in all other files?
    Most likely yes. Note that you only need to include that header in files that actually use the struct type, and your header should have header inclusion guards in case you (indirectly) include this header more than once.

    Quote Originally Posted by livin
    or should the struct be defined in a .cpp file since it is actual definition? if so how do other files see it?
    Most likely no. It can be done this way, but I doubt it is what is asked of you since this is your "first cpp assignment which requires (you) to break the code into multiple files". If you're interested to know, search the Web for "pimpl idiom" and "opaque pointer".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    45
    Thanks a lot..that helped.

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by livin View Post
    or should the struct be defined in a .cpp file since it is actual definition?
    A struct is not a definition. It's a declaration. A declaration specifies the interpretation of a name. A definition is a declaration that also allocates storage.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by oogabooga
    A struct is not a definition. It's a declaration. A declaration specifies the interpretation of a name. A definition is a declaration that also allocates storage.
    No, a struct is not a declaration. A struct is a type. A struct declaration would be something like this:
    Code:
    struct X;
    and a struct definition would be something like this:
    Code:
    struct X
    {
        int n;
    };
    though of course a struct definition is also a struct declaration.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by laserlight View Post
    No, a struct is not a declaration.
    I suppose so. If it was a declaration then you'd be able to repeat it, but you can't (hence the need for include guards). What threw me is that it doesn't allocate storage.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,893
    Quote Originally Posted by oogabooga
    If it was a declaration then you'd be able to repeat it, but you can't
    You can repeat the declaration of a struct, as long as it is not the definition of the struct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can I easily write multi-core programs in C?
    By advancedk in forum C Programming
    Replies: 2
    Last Post: 08-13-2008, 05:22 PM
  2. Multi-file programs
    By Lord Xela in forum C Programming
    Replies: 2
    Last Post: 03-17-2006, 02:19 AM
  3. Multi-language programs?
    By Raeliean in forum C++ Programming
    Replies: 18
    Last Post: 07-10-2005, 12:25 AM
  4. Multi File Projects
    By Travis Dane in forum C++ Programming
    Replies: 12
    Last Post: 12-25-2002, 06:48 PM
  5. multi file i/o
    By Screamager in forum C Programming
    Replies: 10
    Last Post: 02-25-2002, 02:21 PM

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