Vectors

This is a discussion on Vectors within the C++ Programming forums, part of the General Programming Boards category; Ok so I have started to use vectors in my game now. Im using one to make my players inventory. ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128

    Vectors

    Ok so I have started to use vectors in my game now. Im using one to make my players inventory.

    I want to have my main code structure of my game in 1 file and all the inventory in another file etc.

    Now I have wrote out some of my vector for the game

    Code:
    #include <vector>
    #include <string>
    
        vector<string> inventory;
        inventory.push_back("Wooden Mallet");
        inventory.push_back("Battered Shield");
        inventory.push_back("Old Cloak");
    
        vector<string>::iterator myIterator;
        vector<string>::const_iterator iterate;
    Im not sure if to put it into a header file or a cpp file or just put it straight into my int main file...

    If I put it into a external file I am not sure how to write it out so it slots in...

    any suggestions?

  2. #2
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    My opinion is to create classes for all of the real world categories that you're dealing with.... like an inventory class or an object class that stores all of the statistics for that object e.g. strength, lifetime, etc...

    By all means, "wack" the code that you have posted into an inventory class, and create functions for picking items up, discarding them or using them. I would also try and put each class into a different header file, and define the functions that belong to them into co-existing .cpp files.

    [edit]The rule of thumb is to place declaration code inside a header file, and definition code inside a .cpp file (or source file, depending on what language you are programming in )[/edit]

    --
    Legit
    Last edited by legit; 06-21-2009 at 08:16 AM.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    I advise declaring iterator(s) in each place that you use them. The don't need to exist for the lifetime of the object they are used to iterate over.
    I also tend to call them just "iter" or "it", and would possibly do a typedef for vector<string>, perhaps called "StringVec", or "Strings". That all makes it less typing each time you declare one.

    Only put in the header file that which need to be made available to other cpp files.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Ok thank you both for the input. Yeah putting the stuff needed by other cpp files makes sence.

    Im not so sure about type defs but I will look into it

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    So something like this.... (This is massively messy n stuff but just a rough estimate)

    Code:
    #ifndef PLAYERINVENT_H_INCLUDED
    #define PLAYERINVENT_H_INCLUDED
    
    
    
    #endif // PLAYERINVENT_H_INCLUDED
    
    #include <vector>
    #include <string>
    
    using namespace std;
    
    class PlayerInventory
    {
        public:
    
        vector<string> inventory;
        inventory.push_back("Wooden Mallet Weapon");
        inventory.push_back("Battered Shield");
        inventory.push_back("Old Cloak Armor");
    
        vector<string>::iterator myIterator;
        vector<string>::const_iterator iter;
    //void pickUpItem()                                     // not sure if this is correct
    //{                                                     // also not sure how im gonna make this work
    //    if (phrase.find("weapon") == true)
    //    {
    //    inventory[0] = item
    //    }
    //    else if (phrase.find("shield") == true)
    //    {
    //    inventory[1] = item
    //    }
    //    else (phrase.find("Armor") == true)
    //    {
    //    invetory[2] = item
    //    }
    //}
    
    void displayInvent()
    {
        for (iter = invetory.begin(); iter!= inventory.end(); ++iter)
        cout << *iter;
    }
    void pickupItem(itemName)
    {
        inventory.insert(inventory.begin(), itemName);
    }

  6. #6
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    You made a mistake with your inclusion guards...

    Code:
    #ifndef PLAYERINVENT_H_INCLUDED
    #define PLAYERINVENT_H_INCLUDED
    
    
    
    #endif // PLAYERINVENT_H_INCLUDED
    You basically told the compiler that you want the empty space inbetween the preprocessors to never be included multiple times in the same file to avoid compile time errors... You should place your code inbetween the preprocessors:

    Code:
    #ifndef PLAYER_H_INCLUDED
    #define PLAYERINVENT_H_INCLUDED
    
    ... /*Code goes here*/
    
    #endif // PLAYERINVENT_H_INCLUDED
    I'm not sure that you fully understand the concept of them either?
    Last edited by legit; 06-22-2009 at 02:11 PM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> So something like this...

    Not quite. You don't use myIterator anywhere, so remove that whole line. You do use iter, but you use it inside the displayInvent function, so declare it in there.

    Also, I assume you are using pseudo-code. Otherwise, you would have to put the push_back's into a real function and make displayInvent and pickupItem part of the class. One way to do that last part is where you have the "not sure if this is correct". It is a valid way to do things, although usually the code for member functions goes into a cpp file.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    Yeah I would have put the code there but I was a bit rushed for time so I just whacked it in there for an example to ask.

    Also, yeah I am pretty new to c + + and yes, I do not fully understand what it does, neither do I fully understand vectors as it is a new section I am now starting to research.

    Basically I want the player Inventory set as a class then functions to switch items, delete items, empty inventory and all those related functions, then in my main code line I would like to use something along the lines of an if statement,

    would you like to pick up the item.
    if yes then get the pickup function
    if no
    continue to next code.

    and so on, with my other functions

    @ Daved

    It is a valid way to do things, although usually the code for member functions goes into a cpp file.
    Ok I get that, I would just put it as a void function correct? If I do that then why don't I jsut put the other 2 functions in there as well?
    Last edited by Aliaks; 06-23-2009 at 12:49 AM.

  9. #9
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Aliaks View Post
    Also, yeah I am pretty new to c + + and yes, I do not fully understand what it does, neither do I fully understand vectors as it is a new section I am now starting to research.
    Inclusion guards are preprocessors that tell the compiler to never include the code that you place inbetween the Inclusion Guards in the same file more than once. For example, say you had several header files, A.h, B.h, C.h and D.h, and B needed to include A, and C needed to include B and A. If you used the #include preprocessor for the preceding statements, then you would hit a compile time error as the code in A has been included in the same file more than once. The #ifndef preprocessor tells the compiler that if the proceeding statement isn't defined, then look for the #define preprocessor, which comes next. The #define preprocessor tells the compiler to define the following code only once, so when included in the same file multiple times, no more that one definition of the code exists. The #define preprocessor in this context continues until it hits the #endif preprocessor, which tells the compiler that it's finished defining code.

    Although the above example isn't a good one, it demonstrates what I'm putting forward.

    NOTE: My explanation may be wrong, as I'm still relatively new to C++ myself, so if any other, more experienced programmers feel that they need to change something, be my guest.

    Now then, Vectors are container classes that belong to the STL. They are templated classes that allow you to create arrays of an object of your choice. They behave much like standard arrays, but they are safer and more powerful than their predecessors. They allocate memory as they grow, and no manual memory management is needed, therefore they're much safer to use. I can't give much of an insight on vectors although, as I don't really use them, but I'm sure other people on this bored will give a better explanation than mine!

    Quote Originally Posted by Aliaks View Post
    Basically I want the player Inventory set as a class then functions to switch items, delete items, empty inventory and all those related functions, then in my main code line I would like to use something along the lines of an if statement,

    would you like to pick up the item.
    if yes then get the pickup function
    if no
    continue to next code.

    and so on, with my other functions
    I would most probably use if statements, by storing the user input in an std::string, and passing it into them.
    Last edited by legit; 06-23-2009 at 02:09 AM.

  10. #10
    Registered User
    Join Date
    Jun 2009
    Location
    Adeliade, AU
    Posts
    128
    I seeeee
    That is quite interesting, and actually makes sence ! haha

    Thanks very much for that information

  11. #11
    Student legit's Avatar
    Join Date
    Aug 2008
    Location
    UK -> Newcastle
    Posts
    156
    Quote Originally Posted by Aliaks View Post
    I seeeee
    That is quite interesting, and actually makes sence ! haha

    Thanks very much for that information
    No problem, glad I could be of service.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vectors
    By naseerhaider in forum C++ Programming
    Replies: 11
    Last Post: 05-09-2008, 08:21 AM
  2. How can i made vectors measuring program in DevC++
    By flame82 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 02:05 PM
  3. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  4. How to use Vector's in C++ !?!
    By IndioDoido in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2007, 11:13 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 01:29 PM

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