Issue with pointers

I am trying to store information in an array of pointers.
I have been trying to solve this issue.
I am unable to store and print out data stored in the class variable "ID"
because the debugger says, "Unable to find memory".

What do I need to do in order to fix this issue?


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
36
37
38
39
40
41
  #include <iostream>
#include <string>
using namespace std;

class Inventory_Item {
private:
	string ID;
	int quantity;
	int price;
	Inventory_Item *dummy[20] = { nullptr };

public:
	void set(string ID) {
		dummy[0]->ID = ID;
		cout << "\nID: "<<dummy[0];
		
		
	 }
	Inventory_Item() {

		for (int i = 0; i < 21; i++) {
			delete dummy[i];
		}
		delete[] dummy;
	}
};
int main() {

	Inventory_Item test;
	string ID = "123456";
	
	test.set(ID);



	

	system("Pause");
	return 0;
	
}
Last edited on
I am trying to store information in an array of pointers.

Why?

I really don't see the need for that "array" inside your class, I would suggest you consider an array of your class in main() instead.

By the way I don't see any "new" so there should be no "delete", since you only delete what you new. Also why would you "delete" inside your class constructor?




A member array like that could make sense in a tree.
1
2
3
4
// Each node on this tree can have up to 20 chlid nodes
struct TreeNode {
private:
	TreeNode* children[20] {};

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;
}

Last edited on
I've been working on it and I was able to fix it.
Thank you for your help.

Also AbstractionAnon
wouldn't the code on line 25 ...

 
inventory[num_items] = new Inventory_Item ("123456");


...look more like?

 
inventory[num_items] = new Inventory_Item* ("123456");
No. new returns a pointer to an inventory item. That is the correct type to store in an array of Inventory_Items.

Your code should give you a compile error due to a type mismatch.


Topic archived. No new replies allowed.