Thread: multi file programs and structs

  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
    28,413
    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".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 03: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, 07:48 PM
  5. multi file i/o
    By Screamager in forum C Programming
    Replies: 10
    Last Post: 02-25-2002, 03:21 PM