Sign in to follow this  

Can't figure out this problem

This topic is 2548 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 have tried and tried again to figure this out, but I am at a loss and hoping to receive some help here at gamedev. When I run in debug it seems to point to

Item::GetName() function, my code you can see below.


#include <iostream>
#include <string>

using namespace std;

//Generic item class
class Item
{
public:
Item()
{
name = "";
price = 0;
}
void SetName(string newName)
{
name = newName;
}
void SetPrice(int amount)
{
price = amount;
}
string GetName()
{
return name;
}
int GetPrice()
{
return price;
}
private:
std::string name;
int price;
};

//Consumable item derived from generic class
class Potion : public Item
{
public:
Potion()
{
quantity = 0;
}
Potion(Potion* newPotion)
{
CreatePotion(newPotion->GetName(), newPotion->GetPrice());
quantity = 1;
}
void CreatePotion(string name, int cost)
{
SetName(name);
SetPrice(cost);
}
void SetQuantity(int amount)
{
quantity = amount;
}
void AddQuantity(int amount)
{
quantity += amount;
}
int GetQuantity()
{
return quantity;
}
private:
int quantity;
};

class Entity
{
public:
void SetName(string newName)
{
name = newName;
}
string GetName()
{
return name;
}
private:
string name;
};

class Player : public Entity
{
public:
Player()
{
//do nothing
}
Player(string name)
{
SetName(name);
}
void AddPotion(Potion* potion)
{
if (potion == NULL)
return;

//check for duplicate
for (int check = 0; check < 15; check++)
{
if (potionList[check] != NULL)
{
if (potionList[check]->GetName() == potion->GetName())
{
//names are same
potionList[check]->AddQuantity(1);
return;
}
}
}

//no duplicates found

for (int loop=0;loop < 15; loop++)
{
if (potionList[loop] == NULL)
{
//create a potion here
potionList[loop] = new Potion(potion);
return;
}
}

cout << "There is no room in your inventory.\n";
cout << endl;
}

Potion** GetPotionList()
{
return potionList;
}
void DisplayPotions()
{
//list potions
cout << "POTIONS\n\n";
Potion** potionlist = GetPotionList();
for (int loop=0;loop < 15;loop++)
{
if (potionlist[loop] != NULL)
{
cout << potionlist[loop]->GetQuantity() << " x " << potionlist[loop]->GetName() << endl;
}
}
}
private:
Potion* potionList[15];
};

int main()
{
bool isRunning = true;

Player* Hero = new Player("Heidi");

Potion* PotionList = new Potion[2];

cout << "Name: " << Hero->GetName() << endl;

PotionList[0].CreatePotion("HP", 10);
PotionList[1].CreatePotion("MP", 5);

for(int i = 0; i < 2; i++)
{
cout << "Name: " << PotionList[i].GetName() << " - Cost: " << PotionList[i].GetPrice() << endl;
}

Hero->AddPotion(&PotionList[0]);
Hero->AddPotion(&PotionList[1]);

return 0;
}

Share this post


Link to post
Share on other sites
Thx for the quick response, what is happening wrong is the program completely bombs out. It doesn't bomb till I call

Hero->AddPotion(&PotionList[0]);
Hero->AddPotion(&PotionList[1]);

In debug mode, it points to Item::GetName()

Share this post


Link to post
Share on other sites
In your AddPotion function:
potionList[check] is never going to == NULL, since you never cleared your potionList[15] to all NULL pointers. Right now they are filled with garbage.
if( garbage != NULL ) is ok, so you then procede to garbage->GetName()

In the constructor, you need to NULL out all the members of that array.

Share this post


Link to post
Share on other sites

This topic is 2548 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this