Stupid Structs Question

This is a discussion on Stupid Structs Question within the C++ Programming forums, part of the General Programming Boards category; How do I make a struct? For example say i'm making a struct named rider, do i need to make ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    Stupid Structs Question

    How do I make a struct? For example say i'm making a struct named rider, do i need to make a rider.h and then a rider.cpp? All i'm doing is going to be creating a constructor for the rider thats all. So would my file be rider.h
    Code:
    #include<string>
    
    using namespace std;
    
    struct rider
    {
        string lastName;
        int tickets;
        int time;
    }
    and then i would just compile that with the main.cpp. OR can i just so the struct rider part in the main.cpp before the int main().

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If your constructor is simple, you can define inside the rider definition and you would not need a rider.cpp file.

    You can put the rider definition in its own header file or in the main.cpp if you will only be using it in main.cpp. If you plan on using it in any other cpp files, you should put it in its own header.

    If you know anything about classes, the answer is exactly the same as what you would do with a class.

  3. #3
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719
    Usually classes/structs go into headers (declarations), and then an implementation file (cpp) accompanies it. It's mainly for organization, splitting the code up into chunks.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Not only that, but then you can include the header file in multiple source files which can then all use it. See, wherever you use the struct/class you need the declaration, the skeleton:
    Code:
    class x {
    public:
        int y;
        x();
    };
    But the implementation
    Code:
    x::x() {
    }
    can only exist in one file. So usually the code that needs to be wherever the class is used (declaration) goes in the header file, while the implementation goes in a separate source file.

    If you only have one source file (or the struct/class is only used in one source file) then none of this is a problem and you can go ahead and get rid of your header file(s).
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Question Probably
    By Kyrin in forum C Programming
    Replies: 2
    Last Post: 05-07-2006, 01:51 AM
  2. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  3. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 12:43 PM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Stupid question: What does Debugger do?
    By napkin111 in forum C++ Programming
    Replies: 6
    Last Post: 05-02-2002, 11:00 PM

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