Line 10: Why does Inventory_Item contain an array of Inventory_Item pointers?
Are you trying to maintain an inventory? If so, your array of Inventory_Item pointers should be outside of Inventory_Item. As
jlb suggested, an array of Inventory_Items in main would be appropriate.
Line 14: You're always setting the ID of the 0th item. That's not how an inventory works.
Line 15: You're trying to cout a pointer. I'm certain that's not what you want. If you want to output an Inventory_Item, then you should have an output method for it. Ideally, you should overload the << operator, but that's a little beyond what you're asking.
Line 21-24: What to you think you're deleting? You're in your constructor. Your array has not been initialized. Did you mean to put this in your destructor?
Perhaps something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
#include <string>
using namespace std;
class Inventory_Item {
private:
string ID;
int quantity = 0;
int price = 0;
public:
void set(const string & _ID)
{ ID = _ID;
quantity = 1;
}
void Print () const
{ cout << "ID: " << ID << endl;
}
};
int main()
{ Inventory_Item * inventory[20];
int num_items = 0;
inventory[num_items] = new Inventory_Item ("123456");
num_items++;
for (int i = 0; i < num_items; i++)
inventory[i]->Print();
system("Pause");
for (int i = 0; i < num_items; i++)
delete inventory[i];
return 0;
}
| |