Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

right

Quick vector question

This topic is 5280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I had a problem where i was trying to access an element of a vector that wasn''t there before - similar to the problem I''m having now. I fixed that problem a few months ago, but when i started messing around with the code again, I apparently made the same mistake and I cant figure out why it keeps crashing.
#include <iostream.h>
#include <conio.h>
#include <vector.h>
#include <string>
using namespace std;

const int Max_Inventory = 99;

class item{
public:
int strength;
string name;
int effect;
};

vector Inventory(5);

bool StoreItem(item item)       //returns false if inventory is full
{
if(Inventory.size()==Max_Inventory)
{
	return false;
}	
Inventory.push_back(item);
return true;
}
int main()
{
item Sword;
Sword.strength = 1;
Sword.name = "sword";
Sword.effect = 0;

item Armor;
Armor.strength = 1;
Armor.name = "Armor";
Armor.effect = 0;

cout<>i;
return 0;
}
 
The code should print out 1 to show that StoreItem worked properly, and then print out the name of the item in inventory slot 0.

Share this post


Link to post
Share on other sites
Advertisement
Um, two things first: Could you use code tags, and that isn't even compilable code in the first place(??).

-Aryoc

[edited by - Aryoc on June 1, 2004 7:20:23 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
sorry, this is for c++. And sorry, I must have copied it wrong. Here it is:


#include <iostream.h>
#include <conio.h>
#include <vector.h>
#include <string>
using namespace std;

const int Max_Inventory = 99;

class item{
public:
int strength;
string name;
int effect;
};

vector<item> Inventory(5);

bool StoreItem(item item) //returns false if inventory is full
{
if(Inventory.size()==Max_Inventory)
{
return false;
}
Inventory.push_back(item);
return true;
}
int main()
{
item Sword;
Sword.strength = 1;
Sword.name = "sword";
Sword.effect = 0;

item Armor;
Armor.strength = 1;
Armor.name = "Armor";
Armor.effect = 0;

cout<<StoreItem(Sword);
cout<<Inventory[0].name;


int i;
cin>>i;
return 0;
}

Share this post


Link to post
Share on other sites

bool StoreItem(item itemObj){

if(Inventory.size()==Max_Inventory)
return false;

Inventory.push_back(itemObj);
return true;
}


Just an observation, but I don't believe you are allowed to have a variable/parameter name set as "item" when "item" is already defined as a class/struct. Try calling it itemObj or something instead.

Edit: Stuffed up my tags

[edited by - wavarian on June 1, 2004 10:24:23 PM]

[edited by - wavarian on June 1, 2004 10:25:45 PM]

Share this post


Link to post
Share on other sites
First, use:
#include <vector>
#include <iostream>
#include <conio>

instead of:
#include <vector.h>
#include <iostream.h>
#include <conio.h>

The latter are not maintained anymore. (I believe some STL libraries not even contain them anymore ?)

Anyway the reason why the program is not giving you the output you are exepcting is because this line:
vector Inventory(5);
fills your newly created Inventory vector with 5 uninitialized items.
(at positions 0...4 )

So when you add your sword to the inventory it gets stored at position 5.
try this:
cout << Inventory[5].name;

or if you want the last item added to the Inventory:
cout << Inventory.back().name;


Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Direct4D
First, use:
#include &lt;vector>
#include &lt;iostream>
#include &lt;conio>

instead of:
#include &lt;vector.h>
#include &lt;iostream.h>
#include &lt;conio.h>

The latter are not maintained anymore. (I believe some STL libraries not even contain them anymore ?)

Anyway the reason why the program is not giving you the output you are exepcting is because this line:
vector<item> Inventory(5);
fills your newly created Inventory vector with 5 uninitialized items.
(at positions 0...4 )

So when you add your sword to the inventory it gets stored at position 5.
try this:
cout << Inventory[5].name;

or if you want the last item added to the Inventory:
cout << Inventory.back().name;





inventory[5].name doesnt work for me either, i get the same error

Share this post


Link to post
Share on other sites
post using {source} {/source} tags replacing { with [. You can go and edit your previous posts to make them readable.

push_back will increase the size of the vector.

your items will be default constructed because you haven''t written one yourself. this means the integers will be uninitialised (the string will construct itself correctly as it has a default constructor defined). make a constructor and initialise the values to some default. then you might have more success printing them out.

show us the exact code that is making it crash. use source tags. if you don''t know how, read the faq at the top of the page.

Share this post


Link to post
Share on other sites
vector Inventory(5);

why are you declaring your std::vector like this?

The advantage to this vector class is that you can dynamically add elements and not worry about defining a size.


vector<item> Inventory;


Thats how I would declare it, that way when you push_back(item) you wont get access violations.

______________________________________

what we do in life, echoes in eternity


[edited by - Lokken on June 2, 2004 8:41:52 AM]

Share this post


Link to post
Share on other sites
Your Item class has a string object... And when you make a push_back or you pass the object to a function it uses it''s copy constructor... When there is none it just makes a copy like when you use a structures ... this means bit to bit copy... Which is very bad when you use std::string.

In fact by copying a std::string bit per bit you do not copy the char array that is behind, but the pointer that is pointing to that array. This means that your copied object and your original object have each a pointer pointing to the same memory address... And when your copied object is beening destroyed when you exit your function, it releases the memory allocated by the other std::string...

The only solution is to make the operator= and copy constructor for this class like here :


class item {
public:
int strength;
string name;
int effect;

item& operator= (const item& inItem ) {
strength = inItem .strength;
name = inItem .name;
effect = inItem .effect;
}

item (const item& inItem ) {
strength = inItem .strength;
name = inItem .name;
effect = inItem .effect;
}


};



You call an explicit operator = on std::string which does all the right work for you.

If you need more precisions, I might be able to help you... just ask.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <vector>
#include <string>

class Item{
public:
Item() : m_strength(0), m_effect(0) {
}
Item(int strength, const std::string& name, int effect) :
m_strength(strength), m_name(name), m_effect(effect) {
}
int m_strength;
std::string m_name;
int m_effect;
};

typedef std::vector<item> Items;
Items inventory;
const int Max_Inventory = 99;

bool StoreItem(const Item& item) //returns false if inventory is full

{
if(inventory.size()==Max_Inventory)
return false;

inventory.push_back(item);
return true;
}

int main()
{
Item sword(1, "sword", 0);
Item armor(1, "armour", 0);

std::cout << StoreItem(sword);
std::cout << inventory[0].name;

int i;
std::cin>>i;
return 0;
}

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!