Lost Password?

Go Back   CodeCall Programming Forum > Software Development > C and C++

Unregistered, Check out the Coder Battles in the Announcement and Game forums.

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-2008, 08:14 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Credits: 0
Rep Power: 3
MerakSpielman is on a distinguished road
Default Creating vectors in objects?

I'm having a bit of trouble with this. I'm creating a cute little text adventure game for practice, so I can learn the basics of coding in C++.

I decided to use a vector for player inventory. It works great. It's a vector of objects, and I can put in or take out objects, or create a list of them for the "inventory" command, and it all works great.

But I tried to do the same thing for my rooms, so they could have their own "inventories", and it didn't work. The only way I can think of that these two vectors are different is that one is contained in an object and one isn't.

Specifically, I tossed this line into my Global Variables section:

Code:
vector<Object> player_inventory;
"Object" is a class I've defined for objects in the game environment. It contains some basic data, like their name, weight, and description.

When I run my command to list the player's inventory, I call this function:

Code:
int Inventory_List()
{
    cout << "\nYou are carrying:\n";
    if (player_inventory.size() == 0) {cout << "\nNothing\n";};  //checks whether there are no objects in the player's inventory
    if (player_inventory.size() > 0)
    {
       int n;
       n = player_inventory.size();
       do 
        {
                                n--; // this is necessary because the elements in the vector are numbered starting with 0
                                cout << endl << player_inventory[n].title;
                                
        } while (n != 0);
        cout << endl << endl;
    };  
}
And it works fine.

So, since "cout << player_inventory[n].title" successfully outputted the title of an object in the player_inventory vector, I tried to use identical code to output the title of objects in the room's inventory:

Code:
cout << Room_001.room_contents[n].title
But this code returns nothing. It's basically identical to my player inventory code, but I've changed the names. Everything is declared and I get no compilation errors. I've added objects into the room_contents vector. If I add those same objects to the player inventory, they'll list just fine.

So the only difference I can think of is that my player_inventory vector is a global vector, but my room_contents vector is inside a "Room" object. Should that really make a difference?

I'm sorry if my code snippits are a bit disjointed, but I think you'll get the jist.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 03-29-2008, 03:14 AM
v0id's Avatar   
v0id v0id is offline
Super Moderator
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,578
Last Blog:
CherryPy(thon)
Credits: 55
Rep Power: 28
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default Re: Creating vectors in objects?

Quote:
Originally Posted by MerakSpielman
So the only difference I can think of is that my player_inventory vector is a global vector, but my room_contents vector is inside a "Room" object. Should that really make a difference?
No, it shouldn't. If you've put some objects into the vector inside the same "Room" object, you would still be able to use it, like using the player inventory. It just confuses me, because you say you've made the room inventory almost identical to the player inventory, but it still won't work.

By the way, I noticed you're using the operator[] for your vector; you should rather use vector::at. operator[] does not check whether the given index is out of range or not; vector::at does. If the given index is out of range, vector::at will throw a std:: out_of_range-exception.
Code:
myVector.at(index);
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-30-2008, 05:26 AM
nutario's Avatar   
nutario nutario is offline
Newbie
 
Join Date: Mar 2008
Location: Germany
Posts: 23
Credits: 0
Rep Power: 3
nutario is on a distinguished road
Send a message via ICQ to nutario Send a message via Skype™ to nutario
Default Re: Creating vectors in objects?

While I read your posting, I got an idea:
The vector in your "room" obejct: is it public, too; or did you declare it as private in your variable section?

Can you post the part of the code, which doesn't work?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
create a deck of cards with objects damien Java Help 6 01-11-2008 02:24 PM
Java:Tutorial - Making multiple objects work differently John Java Tutorials 0 01-11-2007 02:10 PM
Java:Tutorial - Adding more objects to your window John Java Tutorials 0 01-11-2007 02:09 PM
Creating an analog clock with ActionScript AfTriX Tutorials 2 01-07-2007 02:19 AM
Objects Nightracer General Programming 8 07-28-2006 08:02 PM


All times are GMT -5. The time now is 12:25 AM.

Contest Stats

Xav ........ 1333.07
MeTh0Dz|Reb0rn ........ 1059.52
John ........ 887.37
morefood2001 ........ 879.43
marwex89 ........ 869.98
WingedPanther ........ 851.68
Brandon W ........ 764.23
chili5 ........ 312.39
Steve.L ........ 254.16
dcs ........ 223.87

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 82%

Ads